Debian Codegraph @

libstdc++-v3/testsuite/23_containers/vector/cons/deduction.cc

raw
1 // Copyright (C) 2017-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-do compile { target c++17 } }
19
20 #include <vector>
21 #include <testsuite_iterators.h>
22 #include <testsuite_allocator.h>
23
24 template<typename T>
25 using input_iterator_seq
26 = __gnu_test::test_container<T, __gnu_test::input_iterator_wrapper>;
27
28 template<typename T, typename U> struct require_same;
29 template<typename T> struct require_same<T, T> { using type = void; };
30
31 template<typename T, typename U>
32 typename require_same<T, U>::type
33 check_type(U&) { }
34
35 void
36 test01()
37 {
38 std::vector<unsigned> s0;
39
40 std::vector s1 = s0;
41 check_type<std::vector<unsigned>>(s1);
42
43 std::vector s2 = std::move(s0);
44 check_type<std::vector<unsigned>>(s2);
45
46 const std::vector s3 = s0;
47 check_type<const std::vector<unsigned>>(s3);
48
49 const std::vector s4 = s3;
50 check_type<const std::vector<unsigned>>(s4);
51 }
52
53 void
54 test01b()
55 {
56 std::vector<bool> s0;
57
58 std::vector s1 = s0;
59 check_type<std::vector<bool>>(s1);
60
61 std::vector s2 = std::move(s0);
62 check_type<std::vector<bool>>(s2);
63
64 const std::vector s3 = s0;
65 check_type<const std::vector<bool>>(s3);
66
67 const std::vector s4 = s3;
68 check_type<const std::vector<bool>>(s4);
69 }
70
71 void
72 test02()
73 {
74 unsigned a[1] = {};
75 input_iterator_seq<unsigned> seq(a);
76
77 std::vector s1(seq.begin(), seq.end());
78 check_type<std::vector<unsigned>>(s1);
79
80 std::vector s2(seq.begin(), seq.end(), std::allocator<unsigned>());
81 check_type<std::vector<unsigned>>(s2);
82
83 std::vector s3(1U, 2L);
84 check_type<std::vector<long>>(s3);
85
86 std::vector s4(1U, 2L, std::allocator<long>());
87 check_type<std::vector<long>>(s4);
88 }
89
90 void
91 test02b()
92 {
93 bool a[1] = {};
94 input_iterator_seq<bool> seq(a);
95
96 std::vector s1(seq.begin(), seq.end());
97 check_type<std::vector<bool>>(s1);
98
99 std::vector s2(seq.begin(), seq.end(), std::allocator<bool>());
100 check_type<std::vector<bool>>(s2);
101
102 std::vector s3(1U, true);
103 check_type<std::vector<bool>>(s3);
104
105 std::vector s4(1U, true, std::allocator<bool>());
106 check_type<std::vector<bool>>(s4);
107 }
108
109 struct Pool;
110
111 template<typename T>
112 struct Alloc : __gnu_test::SimpleAllocator<T>
113 {
114 Alloc(Pool*) { }
115
116 template<typename U>
117 Alloc(const Alloc<U>&) { }
118 };
119
120 void
121 test_p1518r2()
122 {
123 // P1518R2 - Stop overconstraining allocators in container deduction guides.
124 // This is a C++23 feature but we support it for C++17 too.
125
126 using Vector = std::vector<unsigned, Alloc<unsigned>>;
127 Pool* p = nullptr;
128 Vector v(p);
129
130 std::vector s1(v, p);
131 check_type<Vector>(s1);
132
133 std::vector s2(std::move(v), p);
134 check_type<Vector>(s2);
135
136 using BVector = std::vector<bool, Alloc<bool>>;
137 BVector b(p);
138
139 std::vector s3(b, p);
140 check_type<BVector>(s3);
141
142 std::vector s4(std::move(b), p);
143 check_type<BVector>(s4);
144 }