Skip to content

Commit

Permalink
Implement LWG-3919 enumerate_view may invoke UB for sized common no…
Browse files Browse the repository at this point in the history
…n-forward underlying ranges (#4526)
  • Loading branch information
frederick-vs-ja authored Mar 28, 2024
1 parent 53f95ba commit 983b26d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 2 additions & 2 deletions stl/inc/ranges
Original file line number Diff line number Diff line change
Expand Up @@ -6390,7 +6390,7 @@ namespace ranges {
_NODISCARD constexpr auto end() noexcept(_Is_end_nothrow_v<_Vw>) // strengthened
requires (!_Simple_view<_Vw>)
{
if constexpr (common_range<_Vw> && sized_range<_Vw>) {
if constexpr (forward_range<_Vw> && common_range<_Vw> && sized_range<_Vw>) {
return _Iterator<false>{_RANGES end(_Range), _RANGES distance(_Range)};
} else {
return _Sentinel<false>{_RANGES end(_Range)};
Expand All @@ -6400,7 +6400,7 @@ namespace ranges {
_NODISCARD constexpr auto end() const noexcept(_Is_end_nothrow_v<const _Vw>) // strengthened
requires _Range_with_movable_references<const _Vw>
{
if constexpr (common_range<const _Vw> && sized_range<const _Vw>) {
if constexpr (forward_range<const _Vw> && common_range<const _Vw> && sized_range<const _Vw>) {
return _Iterator<true>{_RANGES end(_Range), _RANGES distance(_Range)};
} else {
return _Sentinel<true>{_RANGES end(_Range)};
Expand Down
5 changes: 3 additions & 2 deletions tests/std/tests/P2164R9_views_enumerate/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ constexpr bool test_one(Rng&& rng, Expected&& expected) {
assert((as_const(r).begin() == s) == is_empty);
}

STATIC_ASSERT(common_range<R> == (common_range<V> && sized_range<V>) );
STATIC_ASSERT(common_range<R> == (forward_range<V> && common_range<V> && sized_range<V>) );
if constexpr (common_range<V> && sized_range<V> && bidirectional_range<V>) {
if (!is_empty) {
assert(*prev(s) == *prev(end(expected)));
Expand All @@ -228,7 +228,8 @@ constexpr bool test_one(Rng&& rng, Expected&& expected) {
assert((r.begin() == cs) == is_empty);
}

STATIC_ASSERT(common_range<const R> == (common_range<const V> && sized_range<const V>) );
STATIC_ASSERT(
common_range<const R> == (forward_range<const V> && common_range<const V> && sized_range<const V>) );
if constexpr (common_range<const V> && sized_range<const V> && bidirectional_range<const V>) {
if (!is_empty) {
assert(*prev(cs) == *prev(end(expected)));
Expand Down

0 comments on commit 983b26d

Please sign in to comment.