]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: Remove precondition checks from ranges::subrange
authorJonathan Wakely <jwakely@redhat.com>
Tue, 15 Jun 2021 14:07:25 +0000 (15:07 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 15 Jun 2021 17:20:06 +0000 (18:20 +0100)
commita88fc03ba7e52d9a072f25d42bb9619fedb7892e
tree854d23ea74bfc34c1b755753186904c893b1a3c7
parentcb326a6442f09cb36b05ce556fc91e10bfeb0cf6
libstdc++: Remove precondition checks from ranges::subrange

The assertion in the subrange constructor causes semantic changes,
because the call to ranges::distance performs additional operations that
are not part of the constructor's specification. That will fail to
compile if the iterator is move-only, because the argument to
ranges::distance is passed by value. It will modify the subrange if the
iterator is not a forward iterator, because incrementing the copy also
affects the _M_begin member. Those problems could be prevented by using
if-constexpr to only do the assertion for copyable forward iterators,
but the call to ranges::distance can also prevent the constructor being
usable in constant expressions. If the member initializers are usable in
constant expressions, but iterator increments of equality comparisons
are not, then the checks done by __glibcxx_assert might
make constant evaluation fail.

This change removes the assertion. Additionally, a new typedef is
introduced to simplify the declarations using __make_unsigned_like_t on
the iterator's difference type.

Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
libstdc++-v3/ChangeLog:

* include/bits/ranges_util.h (subrange): Add __size_type typedef
and use it to simplify declarations.
(subrange(i, s, n)): Remove assertion.
* testsuite/std/ranges/subrange/constexpr.cc: New test.
libstdc++-v3/include/bits/ranges_util.h
libstdc++-v3/testsuite/std/ranges/subrange/constexpr.cc [new file with mode: 0644]