Skip to content

Commit

Permalink
Implement LWG-3785 ranges::to is over-constrained on the destinatio…
Browse files Browse the repository at this point in the history
…n type being a range (#3319)
  • Loading branch information
frederick-vs-ja authored Jan 12, 2023
1 parent 9c3aeb2 commit a71aa52
Show file tree
Hide file tree
Showing 2 changed files with 28 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 @@ -7668,7 +7668,8 @@ namespace ranges {
// clang-format on

template <class _Rng, class _Container>
concept _Ref_converts = convertible_to<range_reference_t<_Rng>, range_value_t<_Container>>;
concept _Ref_converts =
(!input_range<_Container>) || convertible_to<range_reference_t<_Rng>, range_value_t<_Container>>;

template <class _Rng, class _Container, class... _Types>
concept _Converts_direct_constructible = _Ref_converts<_Rng, _Container> //
Expand Down
26 changes: 26 additions & 0 deletions tests/std/tests/P1206R7_ranges_to_misc/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <cassert>
#include <cstddef>
#include <optional>
#include <ranges>
#include <vector>

Expand Down Expand Up @@ -136,6 +137,28 @@ constexpr bool test_nested_range() {
return true;
}

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

auto expe1 = ranges::to<std::optional<std::vector<int>>>(vec);
assert(expe1.has_value());
assert(*expe1 == vec);

auto expe2 = vec | ranges::to<std::optional<std::vector<int>>>();
assert(expe2.has_value());
assert(*expe2 == vec);

auto expe3 = ranges::to<std::optional>(vec);
assert(expe3.has_value());
assert(*expe3 == vec);

auto expe4 = vec | ranges::to<std::optional>();
assert(expe4.has_value());
assert(*expe4 == vec);

return true;
}

int main() {
test_reservable();
static_assert(test_reservable());
Expand All @@ -147,4 +170,7 @@ int main() {
#if defined(__clang__) || defined(__EDG__) // TRANSITION, VSO-1588614
static_assert(test_nested_range());
#endif // defined(__clang__) || defined(__EDG__)

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

0 comments on commit a71aa52

Please sign in to comment.