// Verify P2165R4 enhancements to std::tuple. // { dg-do run { target c++23 } } #include #include #include #include #include using std::array; using std::pair; using std::tuple; using std::allocator; using std::allocator_arg_t; using std::allocator_arg; namespace alloc { struct B01; struct B02; } template<> struct std::uses_allocator> : std::true_type { }; template<> struct std::uses_allocator> : std::true_type { }; struct A { }; template class tuple_like_t> constexpr bool test01() { struct B { int m; constexpr B(A&) : m(0) { } constexpr B(A&&) : m(1) { } constexpr B(const A&) : m(2) { } constexpr B(const A&&) : m(3) { } }; // template // constexpr explicit(false) tuple(UTuple&&); tuple_like_t tuple_like; [&] { tuple t3b = tuple_like; VERIFY( std::get<0>(t3b).m == 0 && std::get<1>(t3b).m == 0 && std::get<2>(t3b).m == 0 ); }(); [&] { tuple t3b = std::move(tuple_like); VERIFY( std::get<0>(t3b).m == 1 && std::get<1>(t3b).m == 1 && std::get<2>(t3b).m == 1 ); }(); [&] { tuple t3b = std::as_const(tuple_like); VERIFY( std::get<0>(t3b).m == 2 && std::get<1>(t3b).m == 2 && std::get<2>(t3b).m == 2 ); }(); [&] { tuple t3b = std::move(std::as_const(tuple_like)); VERIFY( std::get<0>(t3b).m == 3 && std::get<1>(t3b).m == 3 && std::get<2>(t3b).m == 3 ); }(); // Verify dangling checks. static_assert( !std::is_constructible_v, tuple_like_t> ); static_assert( !std::is_constructible_v, tuple_like_t> ); static_assert( !std::is_constructible_v, tuple_like_t> ); return true; } namespace alloc { struct B01 { int m; B01(A&); B01(A&&); B01(const A&); B01(const A&&); constexpr B01(allocator_arg_t, allocator, A&) : m(0) { } constexpr B01(allocator_arg_t, allocator, A&&) : m(1) { } constexpr B01(allocator_arg_t, allocator, const A&) : m(2) { } constexpr B01(allocator_arg_t, allocator, const A&&) : m(3) { } }; template class tuple_like_t> constexpr bool test01() { using B = B01; // template // constexpr explicit(false) tuple(allocator_arg_t, const Alloc&, UTuple&&); tuple_like_t tuple_like; [&] { tuple t3b = {allocator_arg, allocator{}, tuple_like}; VERIFY( std::get<0>(t3b).m == 0 && std::get<1>(t3b).m == 0 && std::get<2>(t3b).m == 0 ); }(); [&] { tuple t3b = {allocator_arg, allocator{}, std::move(tuple_like)}; VERIFY( std::get<0>(t3b).m == 1 && std::get<1>(t3b).m == 1 && std::get<2>(t3b).m == 1 ); }(); [&] { tuple t3b = {allocator_arg, allocator{}, std::as_const(tuple_like)}; VERIFY( std::get<0>(t3b).m == 2 && std::get<1>(t3b).m == 2 && std::get<2>(t3b).m == 2 ); }(); [&] { tuple t3b = {allocator_arg, allocator{}, std::move(std::as_const(tuple_like))}; VERIFY( std::get<0>(t3b).m == 3 && std::get<1>(t3b).m == 3 && std::get<2>(t3b).m == 3 ); }(); // Verify dangling checks. static_assert( !std::is_constructible_v, allocator_arg_t, allocator, tuple_like_t> ); static_assert( !std::is_constructible_v, allocator_arg_t, allocator, tuple_like_t> ); static_assert( !std::is_constructible_v, allocator_arg_t, allocator, tuple_like_t> ); return true; } } template class tuple_like_t> constexpr bool test02() { struct B { int m; constexpr explicit B(A&) : m(0) { } constexpr explicit B(A&&) : m(1) { } constexpr explicit B(const A&) : m(2) { } constexpr explicit B(const A&&) : m(3) { } }; // template // constexpr explicit(true) tuple(UTuple&&); static_assert( !std::is_convertible_v, tuple> ); tuple_like_t tuple_like; [&] { tuple t3b{tuple_like}; VERIFY( std::get<0>(t3b).m == 0 && std::get<1>(t3b).m == 0 && std::get<2>(t3b).m == 0 ); }(); [&] { tuple t3b{std::move(tuple_like)}; VERIFY( std::get<0>(t3b).m == 1 && std::get<1>(t3b).m == 1 && std::get<2>(t3b).m == 1 ); }(); [&] { tuple t3b{std::as_const(tuple_like)}; VERIFY( std::get<0>(t3b).m == 2 && std::get<1>(t3b).m == 2 && std::get<2>(t3b).m == 2 ); }(); [&] { tuple t3b{std::move(std::as_const(tuple_like))}; VERIFY( std::get<0>(t3b).m == 3 && std::get<1>(t3b).m == 3 && std::get<2>(t3b).m == 3 ); }(); return true; } namespace alloc { struct B02 { int m; explicit B02(A&); explicit B02(A&&); explicit B02(const A&); explicit B02(const A&&); explicit constexpr B02(allocator_arg_t, allocator, A&) : m(0) { } explicit constexpr B02(allocator_arg_t, allocator, A&&) : m(1) { } explicit constexpr B02(allocator_arg_t, allocator, const A&) : m(2) { } explicit constexpr B02(allocator_arg_t, allocator, const A&&) : m(3) { } }; template class tuple_like_t> constexpr bool test02() { using B = B02; // template // constexpr explicit(true) tuple(allocator_arg_t, const Alloc&, UTuple&&); static_assert( !std::is_convertible_v, tuple> ); tuple_like_t tuple_like; [&] { tuple t3b{allocator_arg, allocator{}, tuple_like}; VERIFY( std::get<0>(t3b).m == 0 && std::get<1>(t3b).m == 0 && std::get<2>(t3b).m == 0 ); }(); [&] { tuple t3b{allocator_arg, allocator{}, std::move(tuple_like)}; VERIFY( std::get<0>(t3b).m == 1 && std::get<1>(t3b).m == 1 && std::get<2>(t3b).m == 1 ); }(); [&] { tuple t3b{allocator_arg, allocator{}, std::as_const(tuple_like)}; VERIFY( std::get<0>(t3b).m == 2 && std::get<1>(t3b).m == 2 && std::get<2>(t3b).m == 2 ); }(); [&] { tuple t3b{allocator_arg, allocator{}, std::move(std::as_const(tuple_like))}; VERIFY( std::get<0>(t3b).m == 3 && std::get<1>(t3b).m == 3 && std::get<2>(t3b).m == 3 ); }(); return true; } } template class tuple_like_t> constexpr bool test03() { struct B { int m; constexpr B& operator=(A&) { m = 0; return *this; } constexpr B& operator=(A&&) { m = 1; return *this; } constexpr B& operator=(const A&) { m = 2; return *this; } constexpr B& operator=(const A&&) { m = 3; return *this; } }; // template // constexpr tuple& operator=(UTuple&&); tuple_like_t tuple_like; tuple t3b; t3b = tuple_like; VERIFY( std::get<0>(t3b).m == 0 && std::get<1>(t3b).m == 0 && std::get<2>(t3b).m == 0 ); t3b = std::move(tuple_like); VERIFY( std::get<0>(t3b).m == 1 && std::get<1>(t3b).m == 1 && std::get<2>(t3b).m == 1 ); t3b = std::as_const(tuple_like); VERIFY( std::get<0>(t3b).m == 2 && std::get<1>(t3b).m == 2 && std::get<2>(t3b).m == 2 ); t3b = std::move(std::as_const(tuple_like)); VERIFY( std::get<0>(t3b).m == 3 && std::get<1>(t3b).m == 3 && std::get<2>(t3b).m == 3 ); return true; } template class tuple_like_t> constexpr bool test04() { struct B { mutable int m; constexpr const B& operator=(A&) const { m = 0; return *this; } constexpr const B& operator=(A&&) const { m = 1; return *this; } constexpr const B& operator=(const A&) const { m = 2; return *this; } constexpr const B& operator=(const A&&) const { m = 3; return *this; } }; // template // constexpr const tuple& operator=(UTuple&&) const; tuple_like_t tuple_like; const tuple t3b; t3b = tuple_like; VERIFY( std::get<0>(t3b).m == 0 && std::get<1>(t3b).m == 0 && std::get<2>(t3b).m == 0 ); t3b = std::move(tuple_like); VERIFY( std::get<0>(t3b).m == 1 && std::get<1>(t3b).m == 1 && std::get<2>(t3b).m == 1 ); t3b = std::as_const(tuple_like); VERIFY( std::get<0>(t3b).m == 2 && std::get<1>(t3b).m == 2 && std::get<2>(t3b).m == 2 ); t3b = std::move(std::as_const(tuple_like)); VERIFY( std::get<0>(t3b).m == 3 && std::get<1>(t3b).m == 3 && std::get<2>(t3b).m == 3 ); return true; } template class tuple_like_t> constexpr bool test05() { // template // constexpr bool operator==(const tuple&, const UTuple&); static_assert( tuple{1, 2, 3} == tuple_like_t{1, 2, 3} ); static_assert( tuple{1, 2, 4} != tuple_like_t{1, 2, 3} ); static_assert( tuple_like_t{1, 2, 3} == tuple{1, 2, 3} ); static_assert( tuple_like_t{1, 2, 3} != tuple{1, 2, 4} ); // template // constexpr bool operator<=>const tuple&, const UTuple&); static_assert( (tuple{1, 2, 3} <=> tuple_like_t{1, 2, 3}) == std::strong_ordering::equal ); static_assert( (tuple{1, 2, 4} <=> tuple_like_t{1, 2, 3}) == std::strong_ordering::greater ); static_assert( (tuple_like_t{1, 2, 3} <=> tuple{1, 2, 3}) == std::strong_ordering::equal ); static_assert( (tuple_like_t{1, 2, 3} <=> tuple{1, 2, 4}) == std::strong_ordering::less ); static_assert( tuple{1, 2, 4} > tuple_like_t{1, 2, 3} ); static_assert( tuple_like_t{1, 2, 3} < tuple{1, 2, 4} ); // template // struct basic_common_reference; static_assert( std::same_as, tuple>, tuple> ); static_assert( std::same_as, tuple_like_t>, tuple> ); // template // struct common_type; static_assert( std::same_as, tuple>, tuple> ); static_assert( std::same_as, tuple_like_t>, tuple> ); return true; } template using tuple_like_array = array; int main() { static_assert( test01() ); static_assert( alloc::test01() ); static_assert( test02() ); static_assert( alloc::test02() ); static_assert( test03() ); static_assert( test04() ); static_assert( test05() ); }