xref: /llvm-project/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_const_move_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>
15 // constexpr const pair& operator=(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<const first_type&, U1> is true and
25 // is_assignable<const second_type&, U2> is true.
26 
27 // clang-format off
28 static_assert( std::is_assignable_v<const std::pair<int&&, int&&>&,
29                                     std::pair<long&&, long&&>&&>);
30 static_assert(!std::is_assignable_v<const std::pair<int, int>&,
31                                     std::pair<long, long>&&>);
32 static_assert(!std::is_assignable_v<const std::pair<int, int&&>&,
33                                     std::pair<long, long&&>&&>);
34 static_assert(!std::is_assignable_v<const std::pair<int&&, int>&,
35                                     std::pair<long&&, long>&&>);
36 
37 static_assert(std::is_assignable_v<
38     const std::pair<AssignableFrom<ConstMoveAssign>, AssignableFrom<ConstMoveAssign>>&,
39     std::pair<ConstMoveAssign, ConstMoveAssign>&&>);
40 
41 static_assert(!std::is_assignable_v<
42     const std::pair<AssignableFrom<MoveAssign>, AssignableFrom<MoveAssign>>&,
43     std::pair<MoveAssign, MoveAssign>&&>);
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     std::pair<int&&, int&&> p1{std::move(i1), std::move(i2)};
54     const std::pair<long&&, long&&> p2{std::move(j1), std::move(j2)};
55     p2 = std::move(p1);
56     assert(p2.first == 1);
57     assert(p2.second == 2);
58   }
59 
60   // user defined const move assignment
61   {
62     std::pair<ConstMoveAssign, ConstMoveAssign> p1{1, 2};
63     const std::pair<AssignableFrom<ConstMoveAssign>, AssignableFrom<ConstMoveAssign>> p2{3, 4};
64     p2 = std::move(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 = std::move(t1);
74     assert(t2.first.v.constMoveAssign == 1);
75     assert(t2.second.v.constMoveAssign == 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