Debian Codegraph @

libcudacxx/test/support/deduction_guides_sfinae_checks.h

raw symbols SCIP
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of libcu++, the C++ Standard Library for your entire system,
4 // under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 // SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef TEST_SUPPORT_DEDUCTION_GUIDES_SFINAE_CHECKS_H
12 #define TEST_SUPPORT_DEDUCTION_GUIDES_SFINAE_CHECKS_H
13
14 #include <cuda/std/__memory_>
15 #include <cuda/std/cstddef>
16 #include <cuda/std/functional>
17 #include <cuda/std/initializer_list>
18 #include <cuda/std/iterator>
19 #include <cuda/std/type_traits>
20 #include <cuda/std/utility>
21
22 #include "test_macros.h"
23
24 // `SFINAEs_away` template variable checks whether the template arguments for
25 // a given template class `Instantiated` can be deduced from the given
26 // constructor parameter types `CtrArgs` using CTAD.
27
28 template <template <typename...> class Instantiated,
29 class... CtrArgs,
30 class = decltype(Instantiated(cuda::std::declval<CtrArgs>()...))>
31 __host__ __device__ cuda::std::false_type SFINAEs_away_impl(int);
32
33 template <template <typename...> class Instantiated, class... CtrArgs>
34 __host__ __device__ cuda::std::true_type SFINAEs_away_impl(...);
35
36 template <template <typename...> class Instantiated, class... CtrArgs>
37 constexpr bool SFINAEs_away = decltype(SFINAEs_away_impl<Instantiated, CtrArgs...>(0))::value;
38
39 // For sequence containers the deduction guides should be SFINAE'd away when
40 // given:
41 // - "bad" input iterators (that is, a type not qualifying as an input
42 // iterator);
43 // - a bad allocator.
44 template <template <typename...> class Container, typename InstantiatedContainer>
45 __host__ __device__ constexpr void SequenceContainerDeductionGuidesSfinaeAway()
46 {
47 using Alloc = cuda::std::allocator<int>;
48 using Iter = int*;
49
50 struct BadAlloc
51 {};
52 // Note: the only requirement in the Standard is that integral types cannot be
53 // considered input iterators; however, this doesn't work for sequence
54 // containers because they have constructors of the form `(size_type count,
55 // const value_type& value)`. These constructors would be used when passing
56 // two integral types and would deduce `value_type` to be an integral type.
57 #ifdef _LIBCUDACXX_VERSION
58 using OutputIter = cuda::std::insert_iterator<InstantiatedContainer>;
59 #endif // _LIBCUDACXX_VERSION
60
61 // (iter, iter)
62 //
63 // Cannot deduce from (BAD_iter, BAD_iter)
64 LIBCPP_STATIC_ASSERT(SFINAEs_away<Container, OutputIter, OutputIter>);
65
66 // (iter, iter, alloc)
67 //
68 // Cannot deduce from (BAD_iter, BAD_iter, alloc)
69 LIBCPP_STATIC_ASSERT(SFINAEs_away<Container, OutputIter, OutputIter, Alloc>);
70 // Cannot deduce from (iter, iter, BAD_alloc)
71 static_assert(SFINAEs_away<Container, Iter, Iter, BadAlloc>);
72
73 // (alloc)
74 //
75 // Cannot deduce from (alloc)
76 static_assert(SFINAEs_away<Container, Alloc>);
77 }
78
79 // For associative containers the deduction guides should be SFINAE'd away when
80 // given:
81 // - "bad" input iterators (that is, a type not qualifying as an input
82 // iterator);
83 // - a bad allocator;
84 // - an allocator in place of a comparator.
85
86 template <template <typename...> class Container, typename InstantiatedContainer>
87 __host__ __device__ constexpr void AssociativeContainerDeductionGuidesSfinaeAway()
88 {
89 using ValueType = typename InstantiatedContainer::value_type;
90 using Comp = cuda::std::less<int>;
91 using Alloc = cuda::std::allocator<ValueType>;
92 using Iter = ValueType*;
93 using InitList = cuda::std::initializer_list<ValueType>;
94
95 struct BadAlloc
96 {};
97 // The only requirement in the Standard is that integral types cannot be
98 // considered input iterators, beyond that it is unspecified.
99 using BadIter = int;
100 #ifdef _LIBCUDACXX_VERSION
101 using OutputIter = cuda::std::insert_iterator<InstantiatedContainer>;
102 #endif // _LIBCUDACXX_VERSION
103 using AllocAsComp = Alloc;
104
105 // (iter, iter)
106 //
107 // Cannot deduce from (BAD_iter, BAD_iter)
108 static_assert(SFINAEs_away<Container, BadIter, BadIter>);
109 LIBCPP_STATIC_ASSERT(SFINAEs_away<Container, OutputIter, OutputIter>);
110
111 // (iter, iter, comp)
112 //
113 // Cannot deduce from (BAD_iter, BAD_iter, comp)
114 static_assert(SFINAEs_away<Container, BadIter, BadIter, Comp>);
115 LIBCPP_STATIC_ASSERT(SFINAEs_away<Container, OutputIter, OutputIter, Comp>);
116
117 // (iter, iter, comp, alloc)
118 //
119 // Cannot deduce from (BAD_iter, BAD_iter, comp, alloc)
120 static_assert(SFINAEs_away<Container, BadIter, BadIter, Comp, Alloc>);
121 LIBCPP_STATIC_ASSERT(SFINAEs_away<Container, OutputIter, OutputIter, Comp, Alloc>);
122 // Cannot deduce from (iter, iter, ALLOC_as_comp, alloc)
123 static_assert(SFINAEs_away<Container, Iter, Iter, AllocAsComp, Alloc>);
124 // Cannot deduce from (iter, iter, comp, BAD_alloc)
125 static_assert(SFINAEs_away<Container, Iter, Iter, Comp, BadAlloc>);
126
127 // (iter, iter, alloc)
128 //
129 // Cannot deduce from (BAD_iter, BAD_iter, alloc)
130 static_assert(SFINAEs_away<Container, BadIter, BadIter, Alloc>);
131 LIBCPP_STATIC_ASSERT(SFINAEs_away<Container, OutputIter, OutputIter, Alloc>);
132 // Note: (iter, iter, BAD_alloc) is interpreted as (iter, iter, comp)
133 // instead and fails upon instantiation. There is no requirement to SFINAE
134 // away bad comparators.
135
136 // (init_list, comp, alloc)
137 //
138 // Cannot deduce from (init_list, ALLOC_as_comp, alloc)
139 static_assert(SFINAEs_away<Container, InitList, AllocAsComp, Alloc>);
140 // Cannot deduce from (init_list, comp, BAD_alloc)
141 static_assert(SFINAEs_away<Container, InitList, Comp, BadAlloc>);
142
143 // (init_list, alloc)
144 //
145 // Note: (init_list, BAD_alloc) is interpreted as (init_list, comp) instead
146 // and fails upon instantiation. There is no requirement to SFINAE away bad
147 // comparators.
148 }
149
150 // For unordered containers the deduction guides should be SFINAE'd away when
151 // given:
152 // - "bad" input iterators (that is, a type not qualifying as an input
153 // iterator);
154 // - a bad allocator;
155 // - a bad hash functor (an integral type in place of a hash);
156 // - an allocator in place of a hash functor;
157 // - an allocator in place of a predicate.
158 template <template <typename...> class Container, typename InstantiatedContainer>
159 __host__ __device__ constexpr void UnorderedContainerDeductionGuidesSfinaeAway()
160 {
161 using ValueType = typename InstantiatedContainer::value_type;
162 using Pred = cuda::std::equal_to<int>;
163 using Hash = cuda::std::hash<int>;
164 using Alloc = cuda::std::allocator<ValueType>;
165 using Iter = ValueType*;
166 using InitList = cuda::std::initializer_list<ValueType>;
167
168 using BadHash = int;
169 struct BadAlloc
170 {};
171 // The only requirement in the Standard is that integral types cannot be
172 // considered input iterators, beyond that it is unspecified.
173 using BadIter = int;
174 #ifdef _LIBCUDACXX_VERSION
175 using OutputIter = cuda::std::insert_iterator<InstantiatedContainer>;
176 #endif // _LIBCUDACXX_VERSION
177 using AllocAsHash = Alloc;
178 using AllocAsPred = Alloc;
179
180 // (iter, iter)
181 //
182 // Cannot deduce from (BAD_iter, BAD_iter)
183 static_assert(SFINAEs_away<Container, BadIter, BadIter>);
184 LIBCPP_STATIC_ASSERT(SFINAEs_away<Container, OutputIter, OutputIter>);
185
186 // (iter, iter, buckets)
187 //
188 // Cannot deduce from (BAD_iter, BAD_iter, buckets)
189 static_assert(SFINAEs_away<Container, BadIter, BadIter, cuda::std::size_t>);
190 LIBCPP_STATIC_ASSERT(SFINAEs_away<Container, OutputIter, OutputIter, cuda::std::size_t>);
191
192 // (iter, iter, buckets, hash)
193 //
194 // Cannot deduce from (BAD_iter, BAD_iter, buckets, hash)
195 static_assert(SFINAEs_away<Container, BadIter, BadIter, cuda::std::size_t, Hash>);
196 LIBCPP_STATIC_ASSERT(SFINAEs_away<Container, OutputIter, OutputIter, cuda::std::size_t, Hash>);
197 // Cannot deduce from (iter, iter, buckets, BAD_hash)
198 static_assert(SFINAEs_away<Container, Iter, Iter, cuda::std::size_t, BadHash>);
199 // Note: (iter, iter, buckets, ALLOC_as_hash) is allowed -- it just calls
200 // (iter, iter, buckets, alloc)
201
202 // (iter, iter, buckets, hash, pred)
203 //
204 // Cannot deduce from (BAD_iter, BAD_iter, buckets, hash, pred)
205 static_assert(SFINAEs_away<Container, BadIter, BadIter, cuda::std::size_t, Hash, Pred>);
206 LIBCPP_STATIC_ASSERT(SFINAEs_away<Container, OutputIter, OutputIter, cuda::std::size_t, Hash, Pred>);
207 // Cannot deduce from (iter, iter, buckets, BAD_hash, pred)
208 static_assert(SFINAEs_away<Container, Iter, Iter, cuda::std::size_t, BadHash, Pred>);
209 // Cannot deduce from (iter, iter, buckets, ALLOC_as_hash, pred)
210 static_assert(SFINAEs_away<Container, Iter, Iter, cuda::std::size_t, AllocAsHash, Pred>);
211 // Note: (iter, iter, buckets, hash, ALLOC_as_pred) is allowed -- it just
212 // calls (iter, iter, buckets, hash, alloc)
213
214 // (iter, iter, buckets, hash, pred, alloc)
215 //
216 // Cannot deduce from (BAD_iter, BAD_iter, buckets, hash, pred, alloc)
217 static_assert(SFINAEs_away<Container, BadIter, BadIter, cuda::std::size_t, Hash, Pred, Alloc>);
218 LIBCPP_STATIC_ASSERT(SFINAEs_away<Container, OutputIter, OutputIter, cuda::std::size_t, Hash, Pred, Alloc>);
219 // Cannot deduce from (iter, iter, buckets, BAD_hash, pred, alloc)
220 static_assert(SFINAEs_away<Container, Iter, Iter, cuda::std::size_t, BadHash, Pred, Alloc>);
221 // Cannot deduce from (iter, iter, buckets, ALLOC_as_hash, pred, alloc)
222 static_assert(SFINAEs_away<Container, Iter, Iter, cuda::std::size_t, AllocAsHash, Pred, Alloc>);
223 // Cannot deduce from (iter, iter, buckets, hash, ALLOC_as_pred, alloc)
224 static_assert(SFINAEs_away<Container, Iter, Iter, cuda::std::size_t, Hash, AllocAsPred, Alloc>);
225 // Cannot deduce from (iter, iter, buckets, hash, pred, BAD_alloc)
226 static_assert(SFINAEs_away<Container, Iter, Iter, cuda::std::size_t, Hash, Pred, BadAlloc>);
227
228 // (iter, iter, buckets, alloc)
229 //
230 // Cannot deduce from (BAD_iter, BAD_iter, buckets, alloc)
231 static_assert(SFINAEs_away<Container, BadIter, BadIter, cuda::std::size_t, Alloc>);
232 LIBCPP_STATIC_ASSERT(SFINAEs_away<Container, OutputIter, OutputIter, cuda::std::size_t, Alloc>);
233 // Note: (iter, iter, buckets, BAD_alloc) is interpreted as (iter, iter,
234 // buckets, hash), which is valid because the only requirement for the hash
235 // parameter is that it's not integral.
236
237 // (iter, iter, alloc)
238 //
239 // Cannot deduce from (BAD_iter, BAD_iter, alloc)
240 static_assert(SFINAEs_away<Container, BadIter, BadIter, Alloc>);
241 LIBCPP_STATIC_ASSERT(SFINAEs_away<Container, OutputIter, OutputIter, Alloc>);
242 // Cannot deduce from (iter, iter, BAD_alloc)
243 static_assert(SFINAEs_away<Container, Iter, Iter, BadAlloc>);
244
245 // (iter, iter, buckets, hash, alloc)
246 //
247 // Cannot deduce from (BAD_iter, BAD_iter, buckets, hash, alloc)
248 static_assert(SFINAEs_away<Container, BadIter, BadIter, cuda::std::size_t, Hash, Alloc>);
249 LIBCPP_STATIC_ASSERT(SFINAEs_away<Container, OutputIter, OutputIter, cuda::std::size_t, Hash, Alloc>);
250 // Cannot deduce from (iter, iter, buckets, BAD_hash, alloc)
251 static_assert(SFINAEs_away<Container, Iter, Iter, cuda::std::size_t, BadHash, Alloc>);
252 // Cannot deduce from (iter, iter, buckets, ALLOC_as_hash, alloc)
253 static_assert(SFINAEs_away<Container, Iter, Iter, cuda::std::size_t, AllocAsHash, Alloc>);
254 // Note: (iter, iter, buckets, hash, BAD_alloc) is interpreted as (iter, iter,
255 // buckets, hash, pred), which is valid because there are no requirements for
256 // the predicate.
257
258 // (init_list, buckets, hash)
259 //
260 // Cannot deduce from (init_list, buckets, BAD_hash)
261 static_assert(SFINAEs_away<Container, InitList, cuda::std::size_t, BadHash>);
262 // Note: (init_list, buckets, ALLOC_as_hash) is interpreted as (init_list,
263 // buckets, alloc), which is valid.
264
265 // (init_list, buckets, hash, pred)
266 //
267 // Cannot deduce from (init_list, buckets, BAD_hash, pred)
268 static_assert(SFINAEs_away<Container, InitList, cuda::std::size_t, BadHash, Pred>);
269 // Cannot deduce from (init_list, buckets, ALLOC_as_hash, pred)
270 static_assert(SFINAEs_away<Container, InitList, cuda::std::size_t, AllocAsHash, Pred>);
271 // Note: (init_list, buckets, hash, ALLOC_as_pred) is interpreted as
272 // (init_list, buckets, hash, alloc), which is valid.
273
274 // (init_list, buckets, hash, pred, alloc)
275 //
276 // Cannot deduce from (init_list, buckets, BAD_hash, pred, alloc)
277 static_assert(SFINAEs_away<Container, InitList, cuda::std::size_t, BadHash, Pred, Alloc>);
278 // Cannot deduce from (init_list, buckets, ALLOC_as_hash, pred, alloc)
279 static_assert(SFINAEs_away<Container, InitList, cuda::std::size_t, AllocAsHash, Pred, Alloc>);
280 // Cannot deduce from (init_list, buckets, hash, ALLOC_as_pred, alloc)
281 static_assert(SFINAEs_away<Container, InitList, cuda::std::size_t, Hash, AllocAsPred, Alloc>);
282 // Cannot deduce from (init_list, buckets, hash, pred, BAD_alloc)
283 static_assert(SFINAEs_away<Container, InitList, cuda::std::size_t, Hash, Pred, BadAlloc>);
284
285 // (init_list, buckets, alloc)
286 //
287 // Note: (init_list, buckets, BAD_alloc) is interpreted as (init_list,
288 // buckets, hash), which is valid because the only requirement for the hash
289 // parameter is that it's not integral.
290
291 // (init_list, buckets, hash, alloc)
292 //
293 // Cannot deduce from (init_list, buckets, BAD_hash, alloc)
294 static_assert(SFINAEs_away<Container, InitList, cuda::std::size_t, BadHash, Alloc>);
295 // Cannot deduce from (init_list, buckets, ALLOC_as_hash, alloc)
296 static_assert(SFINAEs_away<Container, InitList, cuda::std::size_t, AllocAsHash, Alloc>);
297
298 // (init_list, alloc)
299 //
300 // Cannot deduce from (init_list, BAD_alloc)
301 static_assert(SFINAEs_away<Container, InitList, BadAlloc>);
302 }
303
304 #endif // TEST_SUPPORT_DEDUCTION_GUIDES_SFINAE_CHECKS_H