xref: /llvm-project/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/alloc_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 Alloc, class... UTypes>
14 //   tuple(allocator_arg_t, const Alloc& a, tuple<UTypes...>&&);
15 
16 // UNSUPPORTED: c++98, c++03
17 
18 #include <tuple>
19 #include <string>
20 #include <memory>
21 #include <cassert>
22 
23 #include "test_macros.h"
24 #include "allocators.h"
25 #include "../alloc_first.h"
26 #include "../alloc_last.h"
27 
28 struct B
29 {
30     int id_;
31 
32     explicit B(int i) : id_(i) {}
33 
34     virtual ~B() {}
35 };
36 
37 struct D
38     : B
39 {
40     explicit D(int i) : B(i) {}
41 };
42 
43 struct Explicit {
44   int value;
45   explicit Explicit(int x) : value(x) {}
46 };
47 
48 struct Implicit {
49   int value;
50   Implicit(int x) : value(x) {}
51 };
52 
53 int main(int, char**)
54 {
55     {
56         typedef std::tuple<int> T0;
57         typedef std::tuple<alloc_first> T1;
58         T0 t0(2);
59         alloc_first::allocator_constructed = false;
60         T1 t1(std::allocator_arg, A1<int>(5), std::move(t0));
61         assert(alloc_first::allocator_constructed);
62         assert(std::get<0>(t1) == 2);
63     }
64     {
65         typedef std::tuple<std::unique_ptr<D>> T0;
66         typedef std::tuple<std::unique_ptr<B>> T1;
67         T0 t0(std::unique_ptr<D>(new D(3)));
68         T1 t1(std::allocator_arg, A1<int>(5), std::move(t0));
69         assert(std::get<0>(t1)->id_ == 3);
70     }
71     {
72         typedef std::tuple<int, std::unique_ptr<D>> T0;
73         typedef std::tuple<alloc_first, std::unique_ptr<B>> T1;
74         T0 t0(2, std::unique_ptr<D>(new D(3)));
75         alloc_first::allocator_constructed = false;
76         T1 t1(std::allocator_arg, A1<int>(5), std::move(t0));
77         assert(alloc_first::allocator_constructed);
78         assert(std::get<0>(t1) == 2);
79         assert(std::get<1>(t1)->id_ == 3);
80     }
81     {
82         typedef std::tuple<int, int, std::unique_ptr<D>> T0;
83         typedef std::tuple<alloc_last, alloc_first, std::unique_ptr<B>> T1;
84         T0 t0(1, 2, std::unique_ptr<D>(new D(3)));
85         alloc_first::allocator_constructed = false;
86         alloc_last::allocator_constructed = false;
87         T1 t1(std::allocator_arg, A1<int>(5), std::move(t0));
88         assert(alloc_first::allocator_constructed);
89         assert(alloc_last::allocator_constructed);
90         assert(std::get<0>(t1) == 1);
91         assert(std::get<1>(t1) == 2);
92         assert(std::get<2>(t1)->id_ == 3);
93     }
94     {
95         std::tuple<int> t1(42);
96         std::tuple<Explicit> t2{std::allocator_arg, std::allocator<void>{}, std::move(t1)};
97         assert(std::get<0>(t2).value == 42);
98     }
99     {
100         std::tuple<int> t1(42);
101         std::tuple<Implicit> t2 = {std::allocator_arg, std::allocator<void>{}, std::move(t1)};
102         assert(std::get<0>(t2).value == 42);
103     }
104 
105   return 0;
106 }
107