xref: /llvm-project/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/convert_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 // template <class... UTypes> tuple(const tuple<UTypes...>& u);
14 
15 // UNSUPPORTED: 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;
ExplicitExplicit26   explicit Explicit(int x) : value(x) {}
27 };
28 
29 struct Implicit {
30   int value;
ImplicitImplicit31   Implicit(int x) : value(x) {}
32 };
33 
34 struct ExplicitTwo {
ExplicitTwoExplicitTwo35     ExplicitTwo() {}
ExplicitTwoExplicitTwo36     ExplicitTwo(ExplicitTwo const&) {}
ExplicitTwoExplicitTwo37     ExplicitTwo(ExplicitTwo &&) {}
38 
39     template <class T, class = typename std::enable_if<!std::is_same<T, ExplicitTwo>::value>::type>
ExplicitTwoExplicitTwo40     explicit ExplicitTwo(T) {}
41 };
42 
43 struct B
44 {
45     int id_;
46 
BB47     explicit B(int i) : id_(i) {}
48 };
49 
50 struct D
51     : B
52 {
DD53     explicit D(int i) : B(i) {}
54 };
55 
56 #if TEST_STD_VER > 11
57 
58 struct A
59 {
60     int id_;
61 
AA62     constexpr A(int i) : id_(i) {}
operator ==(const A & x,const A & y)63     friend constexpr bool operator==(const A& x, const A& y) {return x.id_ == y.id_;}
64 };
65 
66 struct C
67 {
68     int id_;
69 
CC70     constexpr explicit C(int i) : id_(i) {}
operator ==(const C & x,const C & y)71     friend constexpr bool operator==(const C& x, const C& y) {return x.id_ == y.id_;}
72 };
73 
74 #endif
75 
main(int,char **)76 int main(int, char**)
77 {
78     {
79         typedef std::tuple<long> T0;
80         typedef std::tuple<long long> T1;
81         T0 t0(2);
82         T1 t1 = t0;
83         assert(std::get<0>(t1) == 2);
84     }
85 #if TEST_STD_VER > 11
86     {
87         typedef std::tuple<int> T0;
88         typedef std::tuple<A> T1;
89         constexpr T0 t0(2);
90         constexpr T1 t1 = t0;
91         static_assert(std::get<0>(t1) == 2, "");
92     }
93     {
94         typedef std::tuple<int> T0;
95         typedef std::tuple<C> T1;
96         constexpr T0 t0(2);
97         constexpr T1 t1{t0};
98         static_assert(std::get<0>(t1) == C(2), "");
99     }
100 #endif
101     {
102         typedef std::tuple<long, char> T0;
103         typedef std::tuple<long long, int> T1;
104         T0 t0(2, 'a');
105         T1 t1 = t0;
106         assert(std::get<0>(t1) == 2);
107         assert(std::get<1>(t1) == int('a'));
108     }
109     {
110         typedef std::tuple<long, char, D> T0;
111         typedef std::tuple<long long, int, B> T1;
112         T0 t0(2, 'a', D(3));
113         T1 t1 = t0;
114         assert(std::get<0>(t1) == 2);
115         assert(std::get<1>(t1) == int('a'));
116         assert(std::get<2>(t1).id_ == 3);
117     }
118     {
119         D d(3);
120         typedef std::tuple<long, char, D&> T0;
121         typedef std::tuple<long long, int, B&> T1;
122         T0 t0(2, 'a', d);
123         T1 t1 = t0;
124         d.id_ = 2;
125         assert(std::get<0>(t1) == 2);
126         assert(std::get<1>(t1) == int('a'));
127         assert(std::get<2>(t1).id_ == 2);
128     }
129     {
130         typedef std::tuple<long, char, int> T0;
131         typedef std::tuple<long long, int, B> T1;
132         T0 t0(2, 'a', 3);
133         T1 t1(t0);
134         assert(std::get<0>(t1) == 2);
135         assert(std::get<1>(t1) == int('a'));
136         assert(std::get<2>(t1).id_ == 3);
137     }
138     {
139         const std::tuple<int> t1(42);
140         std::tuple<Explicit> t2(t1);
141         assert(std::get<0>(t2).value == 42);
142     }
143     {
144         const std::tuple<int> t1(42);
145         std::tuple<Implicit> t2 = t1;
146         assert(std::get<0>(t2).value == 42);
147     }
148     {
149         static_assert(std::is_convertible<ExplicitTwo&&, ExplicitTwo>::value, "");
150         static_assert(std::is_convertible<std::tuple<ExplicitTwo&&>&&, const std::tuple<ExplicitTwo>&>::value, "");
151 
152         ExplicitTwo e;
153         std::tuple<ExplicitTwo> t = std::tuple<ExplicitTwo&&>(std::move(e));
154         ((void)t);
155     }
156   return 0;
157 }
158