diff --git a/stl/inc/ranges b/stl/inc/ranges index 8317ea26049..7193a9347f9 100644 --- a/stl/inc/ranges +++ b/stl/inc/ranges @@ -3767,6 +3767,10 @@ namespace ranges { ++_Cur; if (_Cur == _End) { _Trailing_empty = true; + } else if constexpr (!forward_range<_Vw>) { + // LWG-4249: inner iteration can exhaust the base range before the sentinel comparison, + // so mark as trailing empty instead of appearing to be at the sentinel. + _Trailing_empty = true; } } } else { diff --git a/tests/std/tests/P0896R4_views_lazy_split/test.cpp b/tests/std/tests/P0896R4_views_lazy_split/test.cpp index 09b77dff2d8..f85823733b5 100644 --- a/tests/std/tests/P0896R4_views_lazy_split/test.cpp +++ b/tests/std/tests/P0896R4_views_lazy_split/test.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -383,6 +384,24 @@ void test_lwg_4027() { // COMPILE-ONLY static_assert(is_const_v>); } +void test_lwg_4249() { + // Iterating over an inner range advances the shared current, so the outer iterator can only be + // incremented once more before reaching the end; it must not compare equal to default_sentinel + // in between. + istringstream is{"1 0 2"}; + auto plain = views::istream(is) | views::lazy_split(0); + auto j = plain.begin(); + assert(j != plain.end()); + assert(ranges::equal(*j, views::single(1))); + assert(j != plain.end()); + ++j; + assert(j != plain.end()); + assert(ranges::equal(*j, views::single(2))); + assert(j != plain.end()); + ++j; + assert(j == plain.end()); +} + int main() { static_assert(instantiation_test()); instantiation_test(); @@ -392,4 +411,6 @@ int main() { static_assert(test_lwg_3904()); assert(test_lwg_3904()); + + test_lwg_4249(); }