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 // tuple(tuple&& u);
14 
15 // UNSUPPORTED: c++98, c++03
16 
17 #include <tuple>
18 #include <utility>
19 #include <cassert>
20 
21 #include "MoveOnly.h"
22 
23 struct ConstructsWithTupleLeaf
24 {
25     ConstructsWithTupleLeaf() {}
26 
27     ConstructsWithTupleLeaf(ConstructsWithTupleLeaf const &) { assert(false); }
28     ConstructsWithTupleLeaf(ConstructsWithTupleLeaf &&) {}
29 
30     template <class T>
31     ConstructsWithTupleLeaf(T) {
32         static_assert(!std::is_same<T, T>::value,
33                       "Constructor instantiated for type other than int");
34     }
35 };
36 
37 // move_only type which triggers the empty base optimization
38 struct move_only_ebo {
39   move_only_ebo() = default;
40   move_only_ebo(move_only_ebo&&) = default;
41 };
42 
43 // a move_only type which does not trigger the empty base optimization
44 struct move_only_large final {
45   move_only_large() : value(42) {}
46   move_only_large(move_only_large&&) = default;
47   int value;
48 };
49 
50 template <class Elem>
51 void test_sfinae() {
52     using Tup = std::tuple<Elem>;
53     using Alloc = std::allocator<void>;
54     using Tag = std::allocator_arg_t;
55     // special members
56     {
57         static_assert(std::is_default_constructible<Tup>::value, "");
58         static_assert(std::is_move_constructible<Tup>::value, "");
59         static_assert(!std::is_copy_constructible<Tup>::value, "");
60         static_assert(!std::is_constructible<Tup, Tup&>::value, "");
61     }
62     // args constructors
63     {
64         static_assert(std::is_constructible<Tup, Elem&&>::value, "");
65         static_assert(!std::is_constructible<Tup, Elem const&>::value, "");
66         static_assert(!std::is_constructible<Tup, Elem&>::value, "");
67     }
68     // uses-allocator special member constructors
69     {
70         static_assert(std::is_constructible<Tup, Tag, Alloc>::value, "");
71         static_assert(std::is_constructible<Tup, Tag, Alloc, Tup&&>::value, "");
72         static_assert(!std::is_constructible<Tup, Tag, Alloc, Tup const&>::value, "");
73         static_assert(!std::is_constructible<Tup, Tag, Alloc, Tup &>::value, "");
74     }
75     // uses-allocator args constructors
76     {
77         static_assert(std::is_constructible<Tup, Tag, Alloc, Elem&&>::value, "");
78         static_assert(!std::is_constructible<Tup, Tag, Alloc, Elem const&>::value, "");
79         static_assert(!std::is_constructible<Tup, Tag, Alloc, Elem &>::value, "");
80     }
81 }
82 
83 int main(int, char**)
84 {
85     {
86         typedef std::tuple<> T;
87         T t0;
88         T t = std::move(t0);
89         ((void)t); // Prevent unused warning
90     }
91     {
92         typedef std::tuple<MoveOnly> T;
93         T t0(MoveOnly(0));
94         T t = std::move(t0);
95         assert(std::get<0>(t) == 0);
96     }
97     {
98         typedef std::tuple<MoveOnly, MoveOnly> T;
99         T t0(MoveOnly(0), MoveOnly(1));
100         T t = std::move(t0);
101         assert(std::get<0>(t) == 0);
102         assert(std::get<1>(t) == 1);
103     }
104     {
105         typedef std::tuple<MoveOnly, MoveOnly, MoveOnly> T;
106         T t0(MoveOnly(0), MoveOnly(1), MoveOnly(2));
107         T t = std::move(t0);
108         assert(std::get<0>(t) == 0);
109         assert(std::get<1>(t) == 1);
110         assert(std::get<2>(t) == 2);
111     }
112     // A bug in tuple caused __tuple_leaf to use its explicit converting constructor
113     //  as its move constructor. This tests that ConstructsWithTupleLeaf is not called
114     // (w/ __tuple_leaf)
115     {
116         typedef std::tuple<ConstructsWithTupleLeaf> d_t;
117         d_t d((ConstructsWithTupleLeaf()));
118         d_t d2(static_cast<d_t &&>(d));
119     }
120     {
121         test_sfinae<move_only_ebo>();
122         test_sfinae<move_only_large>();
123     }
124 
125   return 0;
126 }
127