| 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 | // <utility> |
| 10 | |
| 11 | // template <class T1, class T2> struct pair |
| 12 | |
| 13 | // template<class U, class V> pair& operator=(const pair<U, V>& p); |
| 14 | |
| 15 | #include <utility> |
| 16 | #include <cassert> |
| 17 | |
| 18 | #include "test_macros.h" |
| 19 | #if TEST_STD_VER >= 11 |
| 20 | #include "archetypes.h" |
| 21 | #endif |
| 22 | |
| 23 | struct CopyAssignableInt { |
| 24 | CopyAssignableInt& operator=(int&) { return *this; } |
| 25 | }; |
| 26 | |
| 27 | struct Unrelated {}; |
| 28 | |
| 29 | TEST_CONSTEXPR_CXX20 bool test() { |
| 30 | { |
| 31 | typedef std::pair<int, short> P1; |
| 32 | typedef std::pair<double, long> P2; |
| 33 | P1 p1(3, static_cast<short>(4)); |
| 34 | P2 p2; |
| 35 | p2 = p1; |
| 36 | assert(p2.first == 3); |
| 37 | assert(p2.second == 4); |
| 38 | } |
| 39 | #if TEST_STD_VER >= 20 |
| 40 | { |
| 41 | using C = ConstexprTestTypes::TestType; |
| 42 | using P = std::pair<int, C>; |
| 43 | using T = std::pair<long, C>; |
| 44 | const T t(42, -42); |
| 45 | P p(101, 101); |
| 46 | p = t; |
| 47 | assert(p.first == 42); |
| 48 | assert(p.second.value == -42); |
| 49 | } |
| 50 | #elif TEST_STD_VER >= 11 |
| 51 | { |
| 52 | using C = TestTypes::TestType; |
| 53 | using P = std::pair<int, C>; |
| 54 | using T = std::pair<long, C>; |
| 55 | const T t(42, -42); |
| 56 | P p(101, 101); |
| 57 | C::reset_constructors(); |
| 58 | p = t; |
| 59 | assert(C::constructed == 0); |
| 60 | assert(C::assigned == 1); |
| 61 | assert(C::copy_assigned == 1); |
| 62 | assert(C::move_assigned == 0); |
| 63 | assert(p.first == 42); |
| 64 | assert(p.second.value == -42); |
| 65 | } |
| 66 | { // test const requirement |
| 67 | using T = std::pair<CopyAssignableInt, CopyAssignableInt>; |
| 68 | using P = std::pair<int, int>; |
| 69 | static_assert(!std::is_assignable<T&, P const>::value, "" ); |
| 70 | } |
| 71 | { |
| 72 | using T = std::pair<int, Unrelated>; |
| 73 | using P = std::pair<Unrelated, int>; |
| 74 | static_assert(!std::is_assignable<T&, P&>::value, "" ); |
| 75 | static_assert(!std::is_assignable<P&, T&>::value, "" ); |
| 76 | } |
| 77 | #endif |
| 78 | #if TEST_STD_VER >= 11 || defined(_LIBCPP_VERSION) // valid in C++11, provided in C++03 with libc++ as an extension |
| 79 | { |
| 80 | int i = 0, j = 0; |
| 81 | std::pair<int&, int&> p(i, j); |
| 82 | const std::pair<const int, const int> from(11, 12); |
| 83 | p = from; |
| 84 | assert(i == 11); |
| 85 | assert(j == 12); |
| 86 | } |
| 87 | #endif |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | int main(int, char**) { |
| 92 | test(); |
| 93 | #if TEST_STD_VER >= 20 |
| 94 | static_assert(test()); |
| 95 | #endif |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |