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 T, class U> struct pair;
14
15// pair(pair const&) = default;
16// pair(pair &&) = default;
17// pair& operator=(pair const&);
18// pair& operator=(pair&&);
19
20// Test that the copy/move constructors and assignment operators are
21// correctly defined or deleted based on the properties of `T` and `U`.
22
23#include <cassert>
24#include <string>
25#include <tuple>
26#include <utility>
27
28#include "archetypes.h"
29
30#include "test_macros.h"
31using namespace ImplicitTypes; // Get implicitly archetypes
32
33namespace ConstructorTest {
34
35template <class T1, bool CanCopy = true, bool CanMove = CanCopy> void test() {
36 using P1 = std::pair<T1, int>;
37 using P2 = std::pair<int, T1>;
38 static_assert(std::is_copy_constructible<P1>::value == CanCopy, "");
39 static_assert(std::is_move_constructible<P1>::value == CanMove, "");
40 static_assert(std::is_copy_constructible<P2>::value == CanCopy, "");
41 static_assert(std::is_move_constructible<P2>::value == CanMove, "");
42};
43
44} // namespace ConstructorTest
45
46void test_constructors_exist() {
47 using namespace ConstructorTest;
48 {
49 test<int>();
50 test<int &>();
51 test<int &&, false, true>();
52 test<const int>();
53 test<const int &>();
54 test<const int &&, false, true>();
55 }
56 {
57 test<Copyable>();
58 test<Copyable &>();
59 test<Copyable &&, false, true>();
60 }
61 {
62 test<NonCopyable, false>();
63 test<NonCopyable &, true>();
64 test<NonCopyable &&, false, true>();
65 }
66 {
67 // Even though CopyOnly has an explicitly deleted move constructor
68 // pair's move constructor is only implicitly deleted and therefore
69 // it doesn't participate in overload resolution.
70 test<CopyOnly, true, true>();
71 test<CopyOnly &, true>();
72 test<CopyOnly &&, false, true>();
73 }
74 {
75 test<MoveOnly, false, true>();
76 test<MoveOnly &, true>();
77 test<MoveOnly &&, false, true>();
78 }
79}
80
81namespace AssignmentOperatorTest {
82
83template <class T1, bool CanCopy = true, bool CanMove = CanCopy> void test() {
84 using P1 = std::pair<T1, int>;
85 using P2 = std::pair<int, T1>;
86 static_assert(std::is_copy_assignable<P1>::value == CanCopy, "");
87 static_assert(std::is_move_assignable<P1>::value == CanMove, "");
88 static_assert(std::is_copy_assignable<P2>::value == CanCopy, "");
89 static_assert(std::is_move_assignable<P2>::value == CanMove, "");
90};
91
92} // namespace AssignmentOperatorTest
93
94void test_assignment_operator_exists() {
95 using namespace AssignmentOperatorTest;
96 {
97 test<int>();
98 test<int &>();
99 test<int &&>();
100 test<const int, false>();
101 test<const int &, false>();
102 test<const int &&, false>();
103 }
104 {
105 test<Copyable>();
106 test<Copyable &>();
107 test<Copyable &&>();
108 }
109 {
110 test<NonCopyable, false>();
111 test<NonCopyable &, false>();
112 test<NonCopyable &&, false>();
113 }
114 {
115 test<CopyOnly, true>();
116 test<CopyOnly &, true>();
117 test<CopyOnly &&, true>();
118 }
119 {
120 test<MoveOnly, false, true>();
121 test<MoveOnly &, false, false>();
122 test<MoveOnly &&, false, true>();
123 }
124}
125
126int main(int, char**) {
127 test_constructors_exist();
128 test_assignment_operator_exists();
129
130 return 0;
131}
132

source code of libcxx/test/std/utilities/utility/pairs/pairs.pair/special_member_generation_test.pass.cpp