xref: /llvm-project/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_const_copy_convert.pass.cpp (revision 8f7ae247828d5e19ddcc7fb4b57a4fd838e653b5)
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 // template<class U1, class U2> constexpr
15 // const pair& operator=(const pair<U1, U2>& p) const;
16 
17 #include <cassert>
18 #include <utility>
19 
20 #include "test_macros.h"
21 #include "copy_move_types.h"
22 
23 // Constraints:
24 // is_assignable_v<const first_type&, const U1&> is true, and
25 // is_assignable_v<const second_type&, const U2&> is true.
26 
27 // clang-format off
28 static_assert( std::is_assignable_v<const std::pair<int&, int&>&,
29                                     const std::pair<long&, long&>&>);
30 static_assert(!std::is_assignable_v<const std::pair<int, int>&,
31                                     const std::pair<long, long>&>);
32 static_assert(!std::is_assignable_v<const std::pair<int, int&>&,
33                                     const std::pair<long, long&>&>);
34 static_assert(!std::is_assignable_v<const std::pair<int&, int>&,
35                                     const std::pair<long&, long>&>);
36 
37 static_assert(std::is_assignable_v<
38     const std::pair<AssignableFrom<ConstCopyAssign>, AssignableFrom<ConstCopyAssign>>&,
39     const std::pair<ConstCopyAssign, ConstCopyAssign>&>);
40 
41 static_assert(!std::is_assignable_v<
42     const std::pair<AssignableFrom<CopyAssign>, AssignableFrom<CopyAssign>>&,
43     const std::pair<CopyAssign, CopyAssign>&>);
44 // clang-format on
45 
test()46 constexpr bool test() {
47   // reference types
48   {
49     int i1  = 1;
50     int i2  = 2;
51     long j1 = 3;
52     long j2 = 4;
53     const std::pair<int&, int&> p1{i1, i2};
54     const std::pair<long&, long&> p2{j1, j2};
55     p2 = p1;
56     assert(p2.first == 1);
57     assert(p2.second == 2);
58   }
59 
60   // user defined const copy assignment
61   {
62     const std::pair<ConstCopyAssign, ConstCopyAssign> p1{1, 2};
63     const std::pair<AssignableFrom<ConstCopyAssign>, AssignableFrom<ConstCopyAssign>> p2{3, 4};
64     p2 = p1;
65     assert(p2.first.v.val == 1);
66     assert(p2.second.v.val == 2);
67   }
68 
69   // The correct assignment operator of the underlying type is used
70   {
71     std::pair<TracedAssignment, TracedAssignment> t1{};
72     const std::pair<AssignableFrom<TracedAssignment>, AssignableFrom<TracedAssignment>> t2{};
73     t2 = t1;
74     assert(t2.first.v.constCopyAssign == 1);
75     assert(t2.second.v.constCopyAssign == 1);
76   }
77 
78   return true;
79 }
80 
main(int,char **)81 int main(int, char**) {
82   test();
83 // gcc cannot have mutable member in constant expression
84 #if !defined(TEST_COMPILER_GCC)
85   static_assert(test());
86 #endif
87   return 0;
88 }
89