Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions stl/inc/__msvc_ranges_to.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1106,9 +1106,10 @@ namespace ranges {
_EXPORT_STD template <class _Container, input_range _Rng, class... _Types>
requires (!view<_Container>)
_NODISCARD constexpr _Container to(_Rng&& _Range, _Types&&... _Args) {
static_assert(!is_const_v<_Container>, "C must not be const. ([range.utility.conv.to])");
static_assert(!is_volatile_v<_Container>, "C must not be volatile. ([range.utility.conv.to])");
static_assert(is_class_v<_Container>, "C must be a class type. ([range.utility.conv.to])");
static_assert(!is_const_v<_Container>, "C must not be const. (N5014 [range.utility.conv.to]/1)");
static_assert(!is_volatile_v<_Container>, "C must not be volatile. (N5014 [range.utility.conv.to]/1)");
static_assert(is_class_v<_Container> || is_union_v<_Container>,
"C must be a class type. (N5014 [range.utility.conv.to]/1)");
if constexpr (_Ref_converts<_Rng, _Container>) {
if constexpr (constructible_from<_Container, _Rng, _Types...>) {
return _Container(_STD forward<_Rng>(_Range), _STD forward<_Types>(_Args)...);
Expand Down Expand Up @@ -1142,7 +1143,7 @@ namespace ranges {
} else {
static_assert(false, "ranges::to requires the result to be constructible from the source range, either "
"by using a suitable constructor, or by inserting each element of the range into "
"the default-constructed object. (N4981 [range.utility.conv.to]/2.1.5)");
"the default-constructed object. (N5014 [range.utility.conv.to]/2.1.5)");
}
} else if constexpr (input_range<range_reference_t<_Rng>>) {
const auto _Xform = [](auto&& _Elem) _STATIC_LAMBDA {
Expand All @@ -1153,15 +1154,15 @@ namespace ranges {
static_assert(false,
"ranges::to requires the elements of the source range to be either implicitly convertible to the "
"elements of the destination container, or be ranges themselves for ranges::to to be applied "
"recursively. (N4981 [range.utility.conv.to]/2.3)");
"recursively. (N5014 [range.utility.conv.to]/2.3)");
}
}

template <class _Container>
struct _To_class_fn {
_STL_INTERNAL_STATIC_ASSERT(!is_const_v<_Container>);
_STL_INTERNAL_STATIC_ASSERT(!is_volatile_v<_Container>);
_STL_INTERNAL_STATIC_ASSERT(is_class_v<_Container>);
_STL_INTERNAL_STATIC_ASSERT(is_class_v<_Container> || is_union_v<_Container>);
_STL_INTERNAL_STATIC_ASSERT(!view<_Container>);

template <input_range _Rng, class... _Types>
Expand All @@ -1176,9 +1177,10 @@ namespace ranges {
_EXPORT_STD template <class _Container, class... _Types>
requires (!view<_Container>)
_NODISCARD constexpr auto to(_Types&&... _Args) {
static_assert(!is_const_v<_Container>, "C must not be const. ([range.utility.conv.adaptors])");
static_assert(!is_volatile_v<_Container>, "C must not be volatile. ([range.utility.conv.adaptors])");
static_assert(is_class_v<_Container>, "C must be a class type. ([range.utility.conv.adaptors])");
static_assert(!is_const_v<_Container>, "C must not be const. (N5014 [range.utility.conv.adaptors]/1)");
static_assert(!is_volatile_v<_Container>, "C must not be volatile. (N5014 [range.utility.conv.adaptors]/1)");
static_assert(is_class_v<_Container> || is_union_v<_Container>,
"C must be a class type. (N5014 [range.utility.conv.adaptors]/1)");
return _Range_closure<_To_class_fn<_Container>, decay_t<_Types>...>{_STD forward<_Types>(_Args)...};
}

Expand Down
58 changes: 58 additions & 0 deletions tests/std/tests/P1206R7_ranges_to_misc/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <memory>
#include <optional>
#include <ranges>
#include <type_traits>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -160,6 +161,60 @@ constexpr bool test_nested_range() {
return true;
}

template <class T, class A = std::allocator<T>>
union union_vector {
std::vector<T, A> vec_;

constexpr union_vector() : vec_() {}
constexpr union_vector(const union_vector& other) : vec_(other.vec_) {}
constexpr union_vector(union_vector&& other) noexcept : vec_(std::move(other.vec_)) {}

template <class U>
requires (!std::same_as<std::remove_cvref_t<U>, union_vector>)
&& (!std::same_as<std::remove_cvref_t<U>, std::vector<T, A>>)
&& requires(U&& u) { std::vector<T, A>(std::forward<U>(u)); }
constexpr explicit union_vector(U&& u) : vec_(std::forward<U>(u)) {}

template <class T1, class T2, class... Ts>
requires requires(T1&& t1, T2&& t2, Ts&&... ts) {
std::vector<T, A>(std::forward<T1>(t1), std::forward<T2>(t2), std::forward<Ts>(ts)...);
}
constexpr union_vector(T1&& t1, T2&& t2, Ts&&... ts)
: vec_(std::forward<T1>(t1), std::forward<T2>(t2), std::forward<Ts>(ts)...) {}

constexpr union_vector& operator=(const union_vector& other) {
vec_ = other.vec_;
return *this;
}
constexpr union_vector& operator=(union_vector&& other)
noexcept(std::is_nothrow_move_assignable_v<std::vector<T, A>>) {
vec_ = std::move(other.vec_);
return *this;
}

constexpr ~union_vector() noexcept {
vec_.~vector();
}
};

template <ranges::input_range R, class A = std::allocator<ranges::range_value_t<R>>>
union_vector(std::from_range_t, R&&, A = A()) -> union_vector<ranges::range_value_t<R>, A>;

constexpr bool test_to_union() {
constexpr int src[]{42, 1729};

assert(ranges::equal(ranges::to<union_vector<long>>(src).vec_, src));
assert(ranges::equal((src | ranges::to<union_vector<long>>()).vec_, src));

static_assert(std::same_as<decltype(ranges::to<union_vector>(src)), union_vector<int>>);
assert(ranges::equal(ranges::to<union_vector>(src).vec_, src));

static_assert(std::same_as<decltype(src | ranges::to<union_vector>()), union_vector<int>>);
assert(ranges::equal((src | ranges::to<union_vector>()).vec_, src));

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))) {}
Expand Down Expand Up @@ -370,6 +425,9 @@ int main() {
test_nested_range();
static_assert(test_nested_range());

test_to_union();
static_assert(test_to_union());

test_lwg3733();
static_assert(test_lwg3733());

Expand Down