]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/tuple/cons/deduction.cc
Avoid unnecessary inclusion of <stdexcept> header
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / tuple / cons / deduction.cc
1 // { dg-options "-std=gnu++17" }
2 // { dg-do compile { target c++17 } }
3
4 // Copyright (C) 2017-2019 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
20
21 #include <tuple>
22 #include <memory>
23
24 template<typename T, typename U> struct require_same;
25 template<typename T> struct require_same<T, T> { using type = void; };
26
27 template<typename T, typename U>
28 typename require_same<T, U>::type
29 check_type(U&) { }
30
31 struct MoveOnly
32 {
33 MoveOnly() = default;
34 MoveOnly(MoveOnly&&);
35 MoveOnly& operator=(MoveOnly&&);
36 };
37
38 void
39 test00()
40 {
41 std::tuple x;
42 check_type<std::tuple<>>(x);
43
44 std::tuple copy = x;
45 check_type<decltype(x)>(copy);
46 std::tuple move = std::move(x);
47 check_type<decltype(x)>(move);
48 }
49
50 void
51 test01()
52 {
53 std::tuple x = 5;
54 check_type<std::tuple<int>>(x);
55 int y = 42;
56 std::tuple x2 = y;
57 check_type<std::tuple<int>>(x2);
58 const int z = 666;
59 std::tuple x3 = z;
60 check_type<std::tuple<int>>(x3);
61 std::tuple mo = MoveOnly();
62 check_type<std::tuple<MoveOnly>>(mo);
63 mo = MoveOnly();
64
65 std::tuple copy = x;
66 check_type<decltype(x)>(copy);
67 std::tuple move = std::move(mo);
68 check_type<decltype(mo)>(move);
69 }
70
71 void
72 test02()
73 {
74 std::tuple x{5, 6u};
75 check_type<std::tuple<int, unsigned>>(x);
76 int y = 42;
77 std::tuple x2{y, 48u};
78 check_type<std::tuple<int, unsigned>>(x2);
79 const int z = 666;
80 std::tuple x3{z, y};
81 check_type<std::tuple<int, int>>(x3);
82 std::tuple x4{1, x};
83 check_type<std::tuple<int, decltype(x)>>(x4);
84 std::tuple mo{MoveOnly(), 2l};
85 check_type<std::tuple<MoveOnly, long>>(mo);
86 mo = {MoveOnly(), 3l};
87
88 std::tuple copy = x;
89 check_type<decltype(x)>(copy);
90 std::tuple copy2{x};
91 check_type<decltype(x)>(copy2);
92 std::tuple move = std::move(mo);
93 check_type<decltype(mo)>(move);
94 }
95
96 void
97 test03()
98 {
99 std::tuple x{5, 6u, '7'};
100 check_type<std::tuple<int, unsigned, char>>(x);
101 int y = 42;
102 std::tuple x2{y, 48u, 54l};
103 check_type<std::tuple<int, unsigned, long>>(x2);
104 const int z = 666;
105 std::tuple x3{z, y, x};
106 check_type<std::tuple<int, int, decltype(x)>>(x3);
107 std::tuple x4{1, x, x2};
108 check_type<std::tuple<int, decltype(x), decltype(x2)>>(x4);
109 std::tuple mo{MoveOnly(), 2l};
110 check_type<std::tuple<MoveOnly, long>>(mo);
111 mo = {MoveOnly(), 3l};
112
113 std::tuple copy = x;
114 check_type<decltype(x)>(copy);
115 std::tuple copy2{x};
116 check_type<decltype(x)>(copy2);
117 std::tuple move = std::move(mo);
118 check_type<decltype(mo)>(move);
119 }
120
121 void
122 test04()
123 {
124 std::pair<int, unsigned> p;
125 std::tuple x = p;
126 check_type<std::tuple<int, unsigned>>(x);
127 int y = 42;
128 std::tuple x2{p};
129 check_type<std::tuple<int, unsigned>>(x2);
130 const int z = 666;
131 std::pair<const int, unsigned> p2;
132 std::tuple x3{p2};
133 check_type<std::tuple<const int, unsigned>>(x3);
134 std::pair<int&, const unsigned&> p3{p.first, p.second};
135 std::tuple x4{p3};
136 check_type<std::tuple<int&, const unsigned&>>(x4);
137 std::tuple mo = std::pair<MoveOnly, MoveOnly>();
138 check_type<std::tuple<MoveOnly, MoveOnly>>(mo);
139
140 std::tuple copy = x4;
141 check_type<decltype(x4)>(copy);
142 std::tuple copy2{x4};
143 check_type<decltype(x4)>(copy2);
144 std::tuple move = std::move(mo);
145 check_type<decltype(mo)>(move);
146 }
147
148 void
149 test05()
150 {
151 std::allocator<double> a;
152 std::tuple x{std::allocator_arg, a, 1};
153 check_type<std::tuple<int>>(x);
154 std::tuple x2{std::allocator_arg, a, 1, '2'};
155 check_type<std::tuple<int, char>>(x2);
156
157 std::pair<float, const short> p{};
158 std::tuple x3{std::allocator_arg, a, p};
159 check_type<std::tuple<float, const short>>(x3);
160 std::tuple x4{std::allocator_arg, a, std::move(p)};
161 check_type<std::tuple<float, const short>>(x4);
162
163 std::tuple x5{std::allocator_arg, a, x};
164 check_type<decltype(x)>(x5);
165 std::tuple x6{std::allocator_arg, a, std::move(x)};
166 check_type<decltype(x)>(x6);
167 }