Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / utilities / utility / pairs / pairs.pair / assign_pair.pass.cpp
blob253bd1b85d03ec51eb911cbcd9d4f9e6d39ba1ab
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03
11 // <utility>
13 // template <class T1, class T2> struct pair
15 // pair& operator=(pair const& p);
17 #include <utility>
18 #include <memory>
19 #include <cassert>
21 #include "test_macros.h"
22 #include "archetypes.h"
24 struct CountAssign {
25 int copied = 0;
26 int moved = 0;
27 TEST_CONSTEXPR_CXX20 CountAssign() = default;
28 TEST_CONSTEXPR_CXX20 CountAssign& operator=(CountAssign const&) {
29 ++copied;
30 return *this;
32 TEST_CONSTEXPR_CXX20 CountAssign& operator=(CountAssign&&) {
33 ++moved;
34 return *this;
38 struct Incomplete;
39 extern Incomplete inc_obj;
41 TEST_CONSTEXPR_CXX20 bool test() {
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);
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);
62 using P = std::pair<int, ConstexprTestTypes::NonCopyable>;
63 static_assert(!std::is_copy_assignable<P>::value, "");
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);
77 using P = std::pair<int, ConstexprTestTypes::MoveAssignOnly>;
78 static_assert(!std::is_copy_assignable<P>::value, "");
81 using P = std::pair<int, std::unique_ptr<int> >;
82 static_assert(!std::is_copy_assignable<P>::value, "");
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);
91 return true;
94 int main(int, char**) {
95 test();
96 #if TEST_STD_VER >= 20
97 static_assert(test());
98 #endif
100 return 0;
103 struct Incomplete {};
104 Incomplete inc_obj;