Skip to content

Commit

Permalink
Implement LWG-3733 ranges::to misuses cpp17-input-iterator (#3496)
Browse files Browse the repository at this point in the history
  • Loading branch information
frederick-vs-ja authored Feb 26, 2023
1 parent 4ba94ca commit 7f25cb3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion stl/inc/ranges
Original file line number Diff line number Diff line change
Expand Up @@ -8390,7 +8390,8 @@ namespace ranges {
template <class _Rng, class _Container, class... _Types>
concept _Converts_and_common_constructible =
_Ref_converts<_Rng, _Container> && common_range<_Rng> //
&& _Cpp17_input_iterator<iterator_t<_Rng>> //
&& requires { typename iterator_traits<iterator_t<_Rng>>::iterator_category; }
&& derived_from<typename iterator_traits<iterator_t<_Rng>>::iterator_category, input_iterator_tag>
&& constructible_from<_Container, iterator_t<_Rng>, iterator_t<_Rng>, _Types...>;

template <class _Container, class _Reference>
Expand Down
24 changes: 24 additions & 0 deletions tests/std/tests/P1206R7_ranges_to_misc/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,27 @@ constexpr bool test_nested_range() {
return true;
}

struct ContainerLike {
template <std::input_iterator Iter>
constexpr ContainerLike(Iter first, Iter last) : dist(static_cast<std::ptrdiff_t>(ranges::distance(first, last))) {}

constexpr char* begin() {
return nullptr;
}
constexpr char* end() {
return nullptr;
}

std::ptrdiff_t dist;
};

constexpr bool test_lwg3733() {
auto nul_termination = std::views::take_while([](char ch) { return ch != '\0'; });
auto c = nul_termination("1729") | std::views::common | ranges::to<ContainerLike>();
assert(c.dist == 4);
return true;
}

constexpr bool test_lwg3785() {
std::vector<int> vec{42, 1729};

Expand Down Expand Up @@ -171,6 +192,9 @@ int main() {
static_assert(test_nested_range());
#endif // defined(__clang__) || defined(__EDG__)

test_lwg3733();
static_assert(test_lwg3733());

test_lwg3785();
static_assert(test_lwg3785());
}

0 comments on commit 7f25cb3

Please sign in to comment.