xref: /llvm-project/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/convert_copy.pass.cpp (revision 2df59c50688c122bbcae7467d3eaf862c3ea3088)
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 // template <class... UTypes> tuple(const tuple<UTypes...>& u);
14 
15 // UNSUPPORTED: c++98, c++03
16 
17 #include <tuple>
18 #include <utility>
19 #include <string>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 
24 struct Explicit {
25   int value;
26   explicit Explicit(int x) : value(x) {}
27 };
28 
29 struct Implicit {
30   int value;
31   Implicit(int x) : value(x) {}
32 };
33 
34 struct B
35 {
36     int id_;
37 
38     explicit B(int i) : id_(i) {}
39 };
40 
41 struct D
42     : B
43 {
44     explicit D(int i) : B(i) {}
45 };
46 
47 #if TEST_STD_VER > 11
48 
49 struct A
50 {
51     int id_;
52 
53     constexpr A(int i) : id_(i) {}
54     friend constexpr bool operator==(const A& x, const A& y) {return x.id_ == y.id_;}
55 };
56 
57 struct C
58 {
59     int id_;
60 
61     constexpr explicit C(int i) : id_(i) {}
62     friend constexpr bool operator==(const C& x, const C& y) {return x.id_ == y.id_;}
63 };
64 
65 #endif
66 
67 int main(int, char**)
68 {
69     {
70         typedef std::tuple<long> T0;
71         typedef std::tuple<long long> T1;
72         T0 t0(2);
73         T1 t1 = t0;
74         assert(std::get<0>(t1) == 2);
75     }
76 #if TEST_STD_VER > 11
77     {
78         typedef std::tuple<int> T0;
79         typedef std::tuple<A> T1;
80         constexpr T0 t0(2);
81         constexpr T1 t1 = t0;
82         static_assert(std::get<0>(t1) == 2, "");
83     }
84     {
85         typedef std::tuple<int> T0;
86         typedef std::tuple<C> T1;
87         constexpr T0 t0(2);
88         constexpr T1 t1{t0};
89         static_assert(std::get<0>(t1) == C(2), "");
90     }
91 #endif
92     {
93         typedef std::tuple<long, char> T0;
94         typedef std::tuple<long long, int> T1;
95         T0 t0(2, 'a');
96         T1 t1 = t0;
97         assert(std::get<0>(t1) == 2);
98         assert(std::get<1>(t1) == int('a'));
99     }
100     {
101         typedef std::tuple<long, char, D> T0;
102         typedef std::tuple<long long, int, B> T1;
103         T0 t0(2, 'a', D(3));
104         T1 t1 = t0;
105         assert(std::get<0>(t1) == 2);
106         assert(std::get<1>(t1) == int('a'));
107         assert(std::get<2>(t1).id_ == 3);
108     }
109     {
110         D d(3);
111         typedef std::tuple<long, char, D&> T0;
112         typedef std::tuple<long long, int, B&> T1;
113         T0 t0(2, 'a', d);
114         T1 t1 = t0;
115         d.id_ = 2;
116         assert(std::get<0>(t1) == 2);
117         assert(std::get<1>(t1) == int('a'));
118         assert(std::get<2>(t1).id_ == 2);
119     }
120     {
121         typedef std::tuple<long, char, int> T0;
122         typedef std::tuple<long long, int, B> T1;
123         T0 t0(2, 'a', 3);
124         T1 t1(t0);
125         assert(std::get<0>(t1) == 2);
126         assert(std::get<1>(t1) == int('a'));
127         assert(std::get<2>(t1).id_ == 3);
128     }
129     {
130         const std::tuple<int> t1(42);
131         std::tuple<Explicit> t2(t1);
132         assert(std::get<0>(t2).value == 42);
133     }
134     {
135         const std::tuple<int> t1(42);
136         std::tuple<Implicit> t2 = t1;
137         assert(std::get<0>(t2).value == 42);
138     }
139 
140   return 0;
141 }
142