xref: /llvm-project/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.assign/copy.pass.cpp (revision 31cbe0f240f660f15602c96b787c58a26f17e179)
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 // <tuple>
10 
11 // template <class... Types> class tuple;
12 
13 // tuple& operator=(const tuple& u);
14 
15 // UNSUPPORTED: c++03
16 
17 #include <tuple>
18 #include <memory>
19 #include <string>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 
24 struct NonAssignable {
25   NonAssignable& operator=(NonAssignable const&) = delete;
26   NonAssignable& operator=(NonAssignable&&) = delete;
27 };
28 struct CopyAssignable {
29   CopyAssignable& operator=(CopyAssignable const&) = default;
30   CopyAssignable& operator=(CopyAssignable &&) = delete;
31 };
32 static_assert(std::is_copy_assignable<CopyAssignable>::value, "");
33 struct MoveAssignable {
34   MoveAssignable& operator=(MoveAssignable const&) = delete;
35   MoveAssignable& operator=(MoveAssignable&&) = default;
36 };
37 
38 int main(int, char**)
39 {
40     {
41         typedef std::tuple<> T;
42         T t0;
43         T t;
44         t = t0;
45     }
46     {
47         typedef std::tuple<int> T;
48         T t0(2);
49         T t;
50         t = t0;
51         assert(std::get<0>(t) == 2);
52     }
53     {
54         typedef std::tuple<int, char> T;
55         T t0(2, 'a');
56         T t;
57         t = t0;
58         assert(std::get<0>(t) == 2);
59         assert(std::get<1>(t) == 'a');
60     }
61     {
62         typedef std::tuple<int, char, std::string> T;
63         const T t0(2, 'a', "some text");
64         T t;
65         t = t0;
66         assert(std::get<0>(t) == 2);
67         assert(std::get<1>(t) == 'a');
68         assert(std::get<2>(t) == "some text");
69     }
70     {
71         // test reference assignment.
72         using T = std::tuple<int&, int&&>;
73         int x = 42;
74         int y = 100;
75         int x2 = -1;
76         int y2 = 500;
77         T t(x, std::move(y));
78         T t2(x2, std::move(y2));
79         t = t2;
80         assert(std::get<0>(t) == x2);
81         assert(&std::get<0>(t) == &x);
82         assert(std::get<1>(t) == y2);
83         assert(&std::get<1>(t) == &y);
84     }
85     {
86         // test that the implicitly generated copy assignment operator
87         // is properly deleted
88         using T = std::tuple<std::unique_ptr<int>>;
89         static_assert(!std::is_copy_assignable<T>::value, "");
90     }
91     {
92         using T = std::tuple<int, NonAssignable>;
93         static_assert(!std::is_copy_assignable<T>::value, "");
94     }
95     {
96         using T = std::tuple<int, CopyAssignable>;
97         static_assert(std::is_copy_assignable<T>::value, "");
98     }
99     {
100         using T = std::tuple<int, MoveAssignable>;
101         static_assert(!std::is_copy_assignable<T>::value, "");
102     }
103 
104   return 0;
105 }
106