| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 10 | |
| 11 | // <utility> |
| 12 | |
| 13 | // template <class T1, class T2> struct pair |
| 14 | |
| 15 | // template <pair-like P> constexpr pair& operator=(P&&); // since C++23 |
| 16 | |
| 17 | #include <array> |
| 18 | #include <cassert> |
| 19 | #include <concepts> |
| 20 | #include <ranges> |
| 21 | #include <string> |
| 22 | #include <tuple> |
| 23 | #include <type_traits> |
| 24 | #include <utility> |
| 25 | |
| 26 | constexpr bool test() { |
| 27 | // Make sure assignment works from array and tuple |
| 28 | { |
| 29 | // Check from std::array |
| 30 | { |
| 31 | std::array<int, 2> a = {1, 2}; |
| 32 | std::pair<int, int> p; |
| 33 | std::same_as<std::pair<int, int>&> decltype(auto) result = (p = a); |
| 34 | assert(&result == &p); |
| 35 | assert(p.first == 1); |
| 36 | assert(p.second == 2); |
| 37 | static_assert(!std::is_assignable_v<std::pair<int, int>&, std::array<int, 1>>); // too small |
| 38 | static_assert( std::is_assignable_v<std::pair<int, int>&, std::array<int, 2>>); // works (test the test) |
| 39 | static_assert(!std::is_assignable_v<std::pair<int, int>&, std::array<int, 3>>); // too large |
| 40 | } |
| 41 | |
| 42 | // Check from std::tuple |
| 43 | { |
| 44 | std::tuple<int, int> a = {1, 2}; |
| 45 | std::pair<int, int> p; |
| 46 | std::same_as<std::pair<int, int>&> decltype(auto) result = (p = a); |
| 47 | assert(&result == &p); |
| 48 | assert(p.first == 1); |
| 49 | assert(p.second == 2); |
| 50 | static_assert(!std::is_assignable_v<std::pair<int, int>&, std::tuple<int>>); // too small |
| 51 | static_assert( std::is_assignable_v<std::pair<int, int>&, std::tuple<int, int>>); // works (test the test) |
| 52 | static_assert(!std::is_assignable_v<std::pair<int, int>&, std::tuple<int, int, int>>); // too large |
| 53 | } |
| 54 | |
| 55 | // Make sure it works for ranges::subrange. This actually deserves an explanation: even though |
| 56 | // the assignment operator explicitly excludes ranges::subrange specializations, such assignments |
| 57 | // end up working because of ranges::subrange's implicit conversion to pair-like types. |
| 58 | // This test ensures that the interoperability works as intended. |
| 59 | { |
| 60 | struct Assignable { |
| 61 | int* ptr = nullptr; |
| 62 | Assignable() = default; |
| 63 | constexpr Assignable(int* p) : ptr(p) { } // enable `subrange::operator pair-like` |
| 64 | constexpr Assignable& operator=(Assignable const&) = default; |
| 65 | }; |
| 66 | int data[] = {1, 2, 3, 4, 5}; |
| 67 | std::ranges::subrange<int*> a(data); |
| 68 | std::pair<Assignable, Assignable> p; |
| 69 | std::same_as<std::pair<Assignable, Assignable>&> decltype(auto) result = (p = a); |
| 70 | assert(&result == &p); |
| 71 | assert(p.first.ptr == data); |
| 72 | assert(p.second.ptr == data + 5); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Make sure we allow element conversion from a pair-like |
| 77 | { |
| 78 | std::tuple<int, char const*> a = {34, "hello world" }; |
| 79 | std::pair<long, std::string> p; |
| 80 | std::same_as<std::pair<long, std::string>&> decltype(auto) result = (p = a); |
| 81 | assert(&result == &p); |
| 82 | assert(p.first == 34); |
| 83 | assert(p.second == std::string("hello world" )); |
| 84 | static_assert(!std::is_assignable_v<std::pair<long, std::string>&, std::tuple<char*, std::string>>); // first not convertible |
| 85 | static_assert(!std::is_assignable_v<std::pair<long, std::string>&, std::tuple<long, void*>>); // second not convertible |
| 86 | static_assert( std::is_assignable_v<std::pair<long, std::string>&, std::tuple<long, std::string>>); // works (test the test) |
| 87 | } |
| 88 | |
| 89 | // Make sure we forward the pair-like elements |
| 90 | { |
| 91 | struct NoCopy { |
| 92 | NoCopy() = default; |
| 93 | NoCopy(NoCopy const&) = delete; |
| 94 | NoCopy(NoCopy&&) = default; |
| 95 | NoCopy& operator=(NoCopy const&) = delete; |
| 96 | NoCopy& operator=(NoCopy&&) = default; |
| 97 | }; |
| 98 | std::tuple<NoCopy, NoCopy> a; |
| 99 | std::pair<NoCopy, NoCopy> p; |
| 100 | p = std::move(a); |
| 101 | } |
| 102 | |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | int main(int, char**) { |
| 107 | test(); |
| 108 | static_assert(test()); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |