xref: /llvm-project/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.assign/convert_copy.pass.cpp (revision 6929bd6d0048134bed6c34ee485900c9d9f2b97b)
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 // Triggers a Clang assertion: https://bugs.llvm.org/show_bug.cgi?id=45879
10 // UNSUPPORTED: clang-13, clang-14
11 
12 // <tuple>
13 
14 // template <class... Types> class tuple;
15 
16 // template <class... UTypes>
17 //   tuple& operator=(const tuple<UTypes...>& u);
18 
19 // UNSUPPORTED: c++03
20 
21 #include <tuple>
22 #include <string>
23 #include <cassert>
24 
25 #include "test_macros.h"
26 
27 struct B {
28     int id_;
29 
30     constexpr explicit B(int i = 0) : id_(i) {}
31 };
32 
33 struct D : B {
34     constexpr explicit D(int i = 0) : B(i) {}
35 };
36 
37 struct NonAssignable {
38     NonAssignable& operator=(NonAssignable const&) = delete;
39     NonAssignable& operator=(NonAssignable&&) = delete;
40 };
41 
42 struct NothrowCopyAssignable {
43     NothrowCopyAssignable& operator=(NothrowCopyAssignable const&) noexcept { return *this; }
44 };
45 
46 struct PotentiallyThrowingCopyAssignable {
47     PotentiallyThrowingCopyAssignable& operator=(PotentiallyThrowingCopyAssignable const&) { return *this; }
48 };
49 
50 TEST_CONSTEXPR_CXX20
51 bool test()
52 {
53     {
54         typedef std::tuple<long> T0;
55         typedef std::tuple<long long> T1;
56         T0 t0(2);
57         T1 t1;
58         t1 = t0;
59         assert(std::get<0>(t1) == 2);
60     }
61     {
62         typedef std::tuple<long, char> T0;
63         typedef std::tuple<long long, int> T1;
64         T0 t0(2, 'a');
65         T1 t1;
66         t1 = t0;
67         assert(std::get<0>(t1) == 2);
68         assert(std::get<1>(t1) == int('a'));
69     }
70     {
71         typedef std::tuple<long, char, D> T0;
72         typedef std::tuple<long long, int, B> T1;
73         T0 t0(2, 'a', D(3));
74         T1 t1;
75         t1 = t0;
76         assert(std::get<0>(t1) == 2);
77         assert(std::get<1>(t1) == int('a'));
78         assert(std::get<2>(t1).id_ == 3);
79     }
80     {
81         D d(3);
82         D d2(2);
83         typedef std::tuple<long, char, D&> T0;
84         typedef std::tuple<long long, int, B&> T1;
85         T0 t0(2, 'a', d2);
86         T1 t1(1, 'b', d);
87         t1 = t0;
88         assert(std::get<0>(t1) == 2);
89         assert(std::get<1>(t1) == int('a'));
90         assert(std::get<2>(t1).id_ == 2);
91     }
92     {
93         // Test that tuple evaluates correctly applies an lvalue reference
94         // before evaluating is_assignable (i.e. 'is_assignable<int&, int&>')
95         // instead of evaluating 'is_assignable<int&&, int&>' which is false.
96         int x = 42;
97         int y = 43;
98         std::tuple<int&&> t(std::move(x));
99         std::tuple<int&> t2(y);
100         t = t2;
101         assert(std::get<0>(t) == 43);
102         assert(&std::get<0>(t) == &x);
103     }
104     return true;
105 }
106 
107 int main(int, char**)
108 {
109     test();
110 #if TEST_STD_VER >= 20
111     static_assert(test());
112 #endif
113 
114     {
115         using T = std::tuple<int, NonAssignable>;
116         using U = std::tuple<NonAssignable, int>;
117         static_assert(!std::is_assignable<T&, U const&>::value, "");
118         static_assert(!std::is_assignable<U&, T const&>::value, "");
119     }
120     {
121         typedef std::tuple<NothrowCopyAssignable, long> T0;
122         typedef std::tuple<NothrowCopyAssignable, int> T1;
123         static_assert(std::is_nothrow_assignable<T0&, T1 const&>::value, "");
124     }
125     {
126         typedef std::tuple<PotentiallyThrowingCopyAssignable, long> T0;
127         typedef std::tuple<PotentiallyThrowingCopyAssignable, int> T1;
128         static_assert(std::is_assignable<T0&, T1 const&>::value, "");
129         static_assert(!std::is_nothrow_assignable<T0&, T1 const&>::value, "");
130     }
131 
132     return 0;
133 }
134