xref: /llvm-project/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_pair.pass.cpp (revision 82c4701d4e7e6c3bb879a5e98d660a126025b87a)
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
10 
11 // <utility>
12 
13 // template <class T1, class T2> struct pair
14 
15 // pair& operator=(pair const& p);
16 
17 #include <utility>
18 #include <memory>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 #include "archetypes.h"
23 
24 struct CountAssign {
25   int copied = 0;
26   int moved = 0;
27   TEST_CONSTEXPR_CXX20 CountAssign() = default;
operator =CountAssign28   TEST_CONSTEXPR_CXX20 CountAssign& operator=(CountAssign const&) {
29     ++copied;
30     return *this;
31   }
operator =CountAssign32   TEST_CONSTEXPR_CXX20 CountAssign& operator=(CountAssign&&) {
33     ++moved;
34     return *this;
35   }
36 };
37 
38 struct Incomplete;
39 extern Incomplete inc_obj;
40 
test()41 TEST_CONSTEXPR_CXX20 bool test() {
42   {
43     typedef std::pair<ConstexprTestTypes::CopyOnly, int> P;
44     const P p1(ConstexprTestTypes::CopyOnly(), short{4});
45     P p2;
46     p2 = p1;
47     assert(p2.second == 4);
48   }
49   {
50     using P = std::pair<int&, int&&>;
51     int x = 42;
52     int y = 101;
53     int x2 = -1;
54     int y2 = 300;
55     P p1(x, std::move(y));
56     P p2(x2, std::move(y2));
57     p1 = p2;
58     assert(p1.first == x2);
59     assert(p1.second == y2);
60   }
61   {
62     using P = std::pair<int, ConstexprTestTypes::NonCopyable>;
63     static_assert(!std::is_copy_assignable<P>::value, "");
64   }
65   {
66     using P = std::pair<CountAssign, ConstexprTestTypes::Copyable>;
67     static_assert(std::is_copy_assignable<P>::value, "");
68     P p;
69     P p2;
70     p = p2;
71     assert(p.first.copied == 1);
72     assert(p.first.moved == 0);
73     assert(p2.first.copied == 0);
74     assert(p2.first.moved == 0);
75   }
76   {
77     using P = std::pair<int, ConstexprTestTypes::MoveAssignOnly>;
78     static_assert(!std::is_copy_assignable<P>::value, "");
79   }
80   {
81     using P = std::pair<int, std::unique_ptr<int> >;
82     static_assert(!std::is_copy_assignable<P>::value, "");
83   }
84   {
85     using P = std::pair<int, Incomplete&>;
86     static_assert(!std::is_copy_assignable<P>::value, "");
87     P p(42, inc_obj);
88     assert(&p.second == &inc_obj);
89   }
90 
91   return true;
92 }
93 
main(int,char **)94 int main(int, char**) {
95   test();
96 #if TEST_STD_VER >= 20
97   static_assert(test());
98 #endif
99 
100   return 0;
101 }
102 
103 struct Incomplete {};
104 Incomplete inc_obj;
105