xref: /llvm-project/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/alloc_convert_copy.pass.cpp (revision 8a78b72289c5b1cb2ea2f0794d5bb9836b0895d8)
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 // constexpr                                        // since c++20
15 //   tuple(allocator_arg_t, const Alloc& a, const tuple<UTypes...>&);
16 
17 // UNSUPPORTED: c++03
18 
19 #include <tuple>
20 #include <memory>
21 #include <cassert>
22 
23 #include "test_macros.h"
24 #include "allocators.h"
25 #include "test_allocator.h"
26 #include "../alloc_first.h"
27 #include "../alloc_last.h"
28 
29 struct Explicit {
30   int value;
ExplicitExplicit31   explicit Explicit(int x) : value(x) {}
32 };
33 
34 struct Implicit {
35   int value;
ImplicitImplicit36   Implicit(int x) : value(x) {}
37 };
38 
39 #if TEST_STD_VER > 17
alloc_copy_constructor_is_constexpr()40 constexpr bool alloc_copy_constructor_is_constexpr() {
41   const std::tuple<int> t1 = 1;
42   std::tuple<int> t2 = {std::allocator_arg, test_allocator<int>{}, t1};
43   assert(std::get<0>(t2) == 1);
44   return true;
45 }
46 #endif
47 
main(int,char **)48 int main(int, char**)
49 {
50     {
51         typedef std::tuple<long> T0;
52         typedef std::tuple<long long> T1;
53         T0 t0(2);
54         T1 t1(std::allocator_arg, A1<int>(), t0);
55         assert(std::get<0>(t1) == 2);
56     }
57     {
58         typedef std::tuple<int> T0;
59         typedef std::tuple<alloc_first> T1;
60         T0 t0(2);
61         alloc_first::allocator_constructed = false;
62         T1 t1(std::allocator_arg, A1<int>(5), t0);
63         assert(alloc_first::allocator_constructed);
64         assert(std::get<0>(t1) == 2);
65     }
66     {
67         typedef std::tuple<int, int> T0;
68         typedef std::tuple<alloc_first, alloc_last> T1;
69         T0 t0(2, 3);
70         alloc_first::allocator_constructed = false;
71         alloc_last::allocator_constructed = false;
72         T1 t1(std::allocator_arg, A1<int>(5), t0);
73         assert(alloc_first::allocator_constructed);
74         assert(alloc_last::allocator_constructed);
75         assert(std::get<0>(t1) == 2);
76         assert(std::get<1>(t1) == 3);
77     }
78     {
79         typedef std::tuple<long, int, int> T0;
80         typedef std::tuple<long long, alloc_first, alloc_last> T1;
81         T0 t0(1, 2, 3);
82         alloc_first::allocator_constructed = false;
83         alloc_last::allocator_constructed = false;
84         T1 t1(std::allocator_arg, A1<int>(5), t0);
85         assert(alloc_first::allocator_constructed);
86         assert(alloc_last::allocator_constructed);
87         assert(std::get<0>(t1) == 1);
88         assert(std::get<1>(t1) == 2);
89         assert(std::get<2>(t1) == 3);
90     }
91     {
92         const std::tuple<int> t1(42);
93         std::tuple<Explicit> t2{std::allocator_arg, std::allocator<int>{},  t1};
94         assert(std::get<0>(t2).value == 42);
95     }
96     {
97         const std::tuple<int> t1(42);
98         std::tuple<Implicit> t2 = {std::allocator_arg, std::allocator<int>{}, t1};
99         assert(std::get<0>(t2).value == 42);
100     }
101     {
102         // Test that we can use a tag derived from allocator_arg_t
103         struct DerivedFromAllocatorArgT : std::allocator_arg_t { };
104         DerivedFromAllocatorArgT derived;
105         std::tuple<long> from(3l);
106         std::tuple<long long> t0(derived, A1<int>(), from);
107     }
108 
109 #if TEST_STD_VER > 17
110     static_assert(alloc_copy_constructor_is_constexpr());
111 #endif
112     return 0;
113 }
114