xref: /llvm-project/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/convert_move.pass.cpp (revision 7fc6a55688c816f5fc1a5481ae7af25be7500356)
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++98, 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 int main(int, char**)
50 {
51     {
52         typedef std::tuple<long> T0;
53         typedef std::tuple<long long> T1;
54         T0 t0(2);
55         T1 t1 = std::move(t0);
56         assert(std::get<0>(t1) == 2);
57     }
58     {
59         typedef std::tuple<long, char> T0;
60         typedef std::tuple<long long, int> T1;
61         T0 t0(2, 'a');
62         T1 t1 = std::move(t0);
63         assert(std::get<0>(t1) == 2);
64         assert(std::get<1>(t1) == int('a'));
65     }
66     {
67         typedef std::tuple<long, char, D> T0;
68         typedef std::tuple<long long, int, B> T1;
69         T0 t0(2, 'a', D(3));
70         T1 t1 = std::move(t0);
71         assert(std::get<0>(t1) == 2);
72         assert(std::get<1>(t1) == int('a'));
73         assert(std::get<2>(t1).id_ == 3);
74     }
75     {
76         D d(3);
77         typedef std::tuple<long, char, D&> T0;
78         typedef std::tuple<long long, int, B&> T1;
79         T0 t0(2, 'a', d);
80         T1 t1 = std::move(t0);
81         d.id_ = 2;
82         assert(std::get<0>(t1) == 2);
83         assert(std::get<1>(t1) == int('a'));
84         assert(std::get<2>(t1).id_ == 2);
85     }
86     {
87         typedef std::tuple<long, char, std::unique_ptr<D>> T0;
88         typedef std::tuple<long long, int, std::unique_ptr<B>> T1;
89         T0 t0(2, 'a', std::unique_ptr<D>(new D(3)));
90         T1 t1 = std::move(t0);
91         assert(std::get<0>(t1) == 2);
92         assert(std::get<1>(t1) == int('a'));
93         assert(std::get<2>(t1)->id_ == 3);
94     }
95     {
96         std::tuple<int> t1(42);
97         std::tuple<Explicit> t2(std::move(t1));
98         assert(std::get<0>(t2).value == 42);
99     }
100     {
101         std::tuple<int> t1(42);
102         std::tuple<Implicit> t2 = std::move(t1);
103         assert(std::get<0>(t2).value == 42);
104     }
105 
106   return 0;
107 }
108