xref: /llvm-project/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/convert_move.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(tuple<UTypes...>&& u);
14 
15 // UNSUPPORTED: c++03
16 
17 #include <tuple>
18 #include <string>
19 #include <memory>
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     virtual ~B() {}
41 };
42 
43 struct D
44     : B
45 {
46     explicit D(int i) : B(i) {}
47 };
48 
49 struct BonkersBananas {
50   template <class T>
51   operator T() &&;
52   template <class T, class = void>
53   explicit operator T() && = delete;
54 };
55 
56 void test_bonkers_bananas_conversion() {
57   using ReturnType = std::tuple<int, int>;
58   static_assert(std::is_convertible<BonkersBananas, ReturnType>(), "");
59   static_assert(!std::is_constructible<ReturnType, BonkersBananas>(), "");
60 
61 }
62 
63 int main(int, char**)
64 {
65     {
66         typedef std::tuple<long> T0;
67         typedef std::tuple<long long> T1;
68         T0 t0(2);
69         T1 t1 = std::move(t0);
70         assert(std::get<0>(t1) == 2);
71     }
72     {
73         typedef std::tuple<long, char> T0;
74         typedef std::tuple<long long, int> T1;
75         T0 t0(2, 'a');
76         T1 t1 = std::move(t0);
77         assert(std::get<0>(t1) == 2);
78         assert(std::get<1>(t1) == int('a'));
79     }
80     {
81         typedef std::tuple<long, char, D> T0;
82         typedef std::tuple<long long, int, B> T1;
83         T0 t0(2, 'a', D(3));
84         T1 t1 = std::move(t0);
85         assert(std::get<0>(t1) == 2);
86         assert(std::get<1>(t1) == int('a'));
87         assert(std::get<2>(t1).id_ == 3);
88     }
89     {
90         D d(3);
91         typedef std::tuple<long, char, D&> T0;
92         typedef std::tuple<long long, int, B&> T1;
93         T0 t0(2, 'a', d);
94         T1 t1 = std::move(t0);
95         d.id_ = 2;
96         assert(std::get<0>(t1) == 2);
97         assert(std::get<1>(t1) == int('a'));
98         assert(std::get<2>(t1).id_ == 2);
99     }
100     {
101         typedef std::tuple<long, char, std::unique_ptr<D>> T0;
102         typedef std::tuple<long long, int, std::unique_ptr<B>> T1;
103         T0 t0(2, 'a', std::unique_ptr<D>(new D(3)));
104         T1 t1 = std::move(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         std::tuple<int> t1(42);
111         std::tuple<Explicit> t2(std::move(t1));
112         assert(std::get<0>(t2).value == 42);
113     }
114     {
115         std::tuple<int> t1(42);
116         std::tuple<Implicit> t2 = std::move(t1);
117         assert(std::get<0>(t2).value == 42);
118     }
119 
120   return 0;
121 }
122