Skip to content

Commit a8e168e

Browse files
authored
[libc++][NFC] Replace structs with variable templates in <__memory/allocator_traits.h> (#129237)
Variable templates are a bit easier on the compiler and improve the readability of the code.
1 parent f6823a0 commit a8e168e

File tree

5 files changed

+64
-84
lines changed

5 files changed

+64
-84
lines changed

libcxx/include/__flat_map/flat_map.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <__functional/invoke.h>
3434
#include <__functional/is_transparent.h>
3535
#include <__functional/operations.h>
36+
#include <__fwd/memory.h>
3637
#include <__fwd/vector.h>
3738
#include <__iterator/concepts.h>
3839
#include <__iterator/distance.h>

libcxx/include/__memory/allocator_traits.h

Lines changed: 49 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,13 @@ using __is_always_equal _LIBCPP_NODEBUG =
151151

152152
// __allocator_traits_rebind
153153
template <class _Tp, class _Up, class = void>
154-
struct __has_rebind_other : false_type {};
154+
inline const bool __has_rebind_other_v = false;
155155
template <class _Tp, class _Up>
156-
struct __has_rebind_other<_Tp, _Up, __void_t<typename _Tp::template rebind<_Up>::other> > : true_type {};
156+
inline const bool __has_rebind_other_v<_Tp, _Up, __void_t<typename _Tp::template rebind<_Up>::other> > = true;
157157

158-
template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
158+
template <class _Tp, class _Up, bool = __has_rebind_other_v<_Tp, _Up> >
159159
struct __allocator_traits_rebind {
160-
static_assert(__has_rebind_other<_Tp, _Up>::value, "This allocator has to implement rebind");
160+
static_assert(__has_rebind_other_v<_Tp, _Up>, "This allocator has to implement rebind");
161161
using type _LIBCPP_NODEBUG = typename _Tp::template rebind<_Up>::other;
162162
};
163163
template <template <class, class...> class _Alloc, class _Tp, class... _Args, class _Up>
@@ -175,53 +175,52 @@ using __allocator_traits_rebind_t _LIBCPP_NODEBUG = typename __allocator_traits_
175175

176176
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
177177

178-
// __has_allocate_hint
178+
// __has_allocate_hint_v
179179
template <class _Alloc, class _SizeType, class _ConstVoidPtr, class = void>
180-
struct __has_allocate_hint : false_type {};
180+
inline const bool __has_allocate_hint_v = false;
181181

182182
template <class _Alloc, class _SizeType, class _ConstVoidPtr>
183-
struct __has_allocate_hint<
183+
inline const bool __has_allocate_hint_v<
184184
_Alloc,
185185
_SizeType,
186186
_ConstVoidPtr,
187-
decltype((void)std::declval<_Alloc>().allocate(std::declval<_SizeType>(), std::declval<_ConstVoidPtr>()))>
188-
: true_type {};
187+
decltype((void)std::declval<_Alloc>().allocate(std::declval<_SizeType>(), std::declval<_ConstVoidPtr>()))> = true;
189188

190-
// __has_construct
189+
// __has_construct_v
191190
template <class, class _Alloc, class... _Args>
192-
struct __has_construct_impl : false_type {};
191+
inline const bool __has_construct_impl = false;
193192

194193
template <class _Alloc, class... _Args>
195-
struct __has_construct_impl<decltype((void)std::declval<_Alloc>().construct(std::declval<_Args>()...)),
196-
_Alloc,
197-
_Args...> : true_type {};
194+
inline const bool
195+
__has_construct_impl<decltype((void)std::declval<_Alloc>().construct(std::declval<_Args>()...)), _Alloc, _Args...> =
196+
true;
198197

199198
template <class _Alloc, class... _Args>
200-
struct __has_construct : __has_construct_impl<void, _Alloc, _Args...> {};
199+
inline const bool __has_construct_v = __has_construct_impl<void, _Alloc, _Args...>;
201200

202-
// __has_destroy
201+
// __has_destroy_v
203202
template <class _Alloc, class _Pointer, class = void>
204-
struct __has_destroy : false_type {};
203+
inline const bool __has_destroy_v = false;
205204

206205
template <class _Alloc, class _Pointer>
207-
struct __has_destroy<_Alloc, _Pointer, decltype((void)std::declval<_Alloc>().destroy(std::declval<_Pointer>()))>
208-
: true_type {};
206+
inline const bool
207+
__has_destroy_v<_Alloc, _Pointer, decltype((void)std::declval<_Alloc>().destroy(std::declval<_Pointer>()))> = true;
209208

210-
// __has_max_size
209+
// __has_max_size_v
211210
template <class _Alloc, class = void>
212-
struct __has_max_size : false_type {};
211+
inline const bool __has_max_size_v = false;
213212

214213
template <class _Alloc>
215-
struct __has_max_size<_Alloc, decltype((void)std::declval<_Alloc&>().max_size())> : true_type {};
214+
inline const bool __has_max_size_v<_Alloc, decltype((void)std::declval<_Alloc&>().max_size())> = true;
216215

217-
// __has_select_on_container_copy_construction
216+
// __has_select_on_container_copy_construction_v
218217
template <class _Alloc, class = void>
219-
struct __has_select_on_container_copy_construction : false_type {};
218+
inline const bool __has_select_on_container_copy_construction_v = false;
220219

221220
template <class _Alloc>
222-
struct __has_select_on_container_copy_construction<
221+
inline const bool __has_select_on_container_copy_construction_v<
223222
_Alloc,
224-
decltype((void)std::declval<_Alloc>().select_on_container_copy_construction())> : true_type {};
223+
decltype((void)std::declval<_Alloc>().select_on_container_copy_construction())> = true;
225224

226225
_LIBCPP_SUPPRESS_DEPRECATED_POP
227226

@@ -272,16 +271,14 @@ struct _LIBCPP_TEMPLATE_VIS allocator_traits {
272271
return __a.allocate(__n);
273272
}
274273

275-
template <class _Ap = _Alloc, __enable_if_t<__has_allocate_hint<_Ap, size_type, const_void_pointer>::value, int> = 0>
274+
template <class _Ap = _Alloc, __enable_if_t<__has_allocate_hint_v<_Ap, size_type, const_void_pointer>, int> = 0>
276275
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer
277276
allocate(allocator_type& __a, size_type __n, const_void_pointer __hint) {
278277
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
279278
return __a.allocate(__n, __hint);
280279
_LIBCPP_SUPPRESS_DEPRECATED_POP
281280
}
282-
template <class _Ap = _Alloc,
283-
class = void,
284-
__enable_if_t<!__has_allocate_hint<_Ap, size_type, const_void_pointer>::value, int> = 0>
281+
template <class _Ap = _Alloc, __enable_if_t<!__has_allocate_hint_v<_Ap, size_type, const_void_pointer>, int> = 0>
285282
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer
286283
allocate(allocator_type& __a, size_type __n, const_void_pointer) {
287284
return __a.allocate(__n);
@@ -304,52 +301,47 @@ struct _LIBCPP_TEMPLATE_VIS allocator_traits {
304301
__a.deallocate(__p, __n);
305302
}
306303

307-
template <class _Tp, class... _Args, __enable_if_t<__has_construct<allocator_type, _Tp*, _Args...>::value, int> = 0>
304+
template <class _Tp, class... _Args, __enable_if_t<__has_construct_v<allocator_type, _Tp*, _Args...>, int> = 0>
308305
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void
309306
construct(allocator_type& __a, _Tp* __p, _Args&&... __args) {
310307
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
311308
__a.construct(__p, std::forward<_Args>(__args)...);
312309
_LIBCPP_SUPPRESS_DEPRECATED_POP
313310
}
314-
template <class _Tp,
315-
class... _Args,
316-
class = void,
317-
__enable_if_t<!__has_construct<allocator_type, _Tp*, _Args...>::value, int> = 0>
311+
template <class _Tp, class... _Args, __enable_if_t<!__has_construct_v<allocator_type, _Tp*, _Args...>, int> = 0>
318312
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void
319313
construct(allocator_type&, _Tp* __p, _Args&&... __args) {
320314
std::__construct_at(__p, std::forward<_Args>(__args)...);
321315
}
322316

323-
template <class _Tp, __enable_if_t<__has_destroy<allocator_type, _Tp*>::value, int> = 0>
317+
template <class _Tp, __enable_if_t<__has_destroy_v<allocator_type, _Tp*>, int> = 0>
324318
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void destroy(allocator_type& __a, _Tp* __p) {
325319
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
326320
__a.destroy(__p);
327321
_LIBCPP_SUPPRESS_DEPRECATED_POP
328322
}
329-
template <class _Tp, class = void, __enable_if_t<!__has_destroy<allocator_type, _Tp*>::value, int> = 0>
323+
template <class _Tp, __enable_if_t<!__has_destroy_v<allocator_type, _Tp*>, int> = 0>
330324
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void destroy(allocator_type&, _Tp* __p) {
331325
std::__destroy_at(__p);
332326
}
333327

334-
template <class _Ap = _Alloc, __enable_if_t<__has_max_size<const _Ap>::value, int> = 0>
328+
template <class _Ap = _Alloc, __enable_if_t<__has_max_size_v<const _Ap>, int> = 0>
335329
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static size_type max_size(const allocator_type& __a) _NOEXCEPT {
336330
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
337331
return __a.max_size();
338332
_LIBCPP_SUPPRESS_DEPRECATED_POP
339333
}
340-
template <class _Ap = _Alloc, class = void, __enable_if_t<!__has_max_size<const _Ap>::value, int> = 0>
334+
template <class _Ap = _Alloc, __enable_if_t<!__has_max_size_v<const _Ap>, int> = 0>
341335
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static size_type max_size(const allocator_type&) _NOEXCEPT {
342336
return numeric_limits<size_type>::max() / sizeof(value_type);
343337
}
344338

345-
template <class _Ap = _Alloc, __enable_if_t<__has_select_on_container_copy_construction<const _Ap>::value, int> = 0>
339+
template <class _Ap = _Alloc, __enable_if_t<__has_select_on_container_copy_construction_v<const _Ap>, int> = 0>
346340
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static allocator_type
347341
select_on_container_copy_construction(const allocator_type& __a) {
348342
return __a.select_on_container_copy_construction();
349343
}
350-
template <class _Ap = _Alloc,
351-
class = void,
352-
__enable_if_t<!__has_select_on_container_copy_construction<const _Ap>::value, int> = 0>
344+
template <class _Ap = _Alloc, __enable_if_t<!__has_select_on_container_copy_construction_v<const _Ap>, int> = 0>
353345
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static allocator_type
354346
select_on_container_copy_construction(const allocator_type& __a) {
355347
return __a;
@@ -372,40 +364,27 @@ struct __check_valid_allocator : true_type {
372364
"original allocator");
373365
};
374366

375-
// __is_default_allocator
367+
// __is_default_allocator_v
376368
template <class _Tp>
377-
struct __is_default_allocator : false_type {};
378-
379-
template <class>
380-
class allocator;
369+
inline const bool __is_std_allocator_v = false;
381370

382371
template <class _Tp>
383-
struct __is_default_allocator<allocator<_Tp> > : true_type {};
384-
385-
// __is_cpp17_move_insertable
386-
template <class _Alloc, class = void>
387-
struct __is_cpp17_move_insertable : is_move_constructible<typename _Alloc::value_type> {};
372+
inline const bool __is_std_allocator_v<allocator<_Tp> > = true;
388373

374+
// __is_cpp17_move_insertable_v
389375
template <class _Alloc>
390-
struct __is_cpp17_move_insertable<
391-
_Alloc,
392-
__enable_if_t< !__is_default_allocator<_Alloc>::value &&
393-
__has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value > >
394-
: true_type {};
395-
396-
// __is_cpp17_copy_insertable
397-
template <class _Alloc, class = void>
398-
struct __is_cpp17_copy_insertable
399-
: integral_constant<bool,
400-
is_copy_constructible<typename _Alloc::value_type>::value &&
401-
__is_cpp17_move_insertable<_Alloc>::value > {};
376+
inline const bool __is_cpp17_move_insertable_v =
377+
is_move_constructible<typename _Alloc::value_type>::value ||
378+
(!__is_std_allocator_v<_Alloc> &&
379+
__has_construct_v<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>);
402380

381+
// __is_cpp17_copy_insertable_v
403382
template <class _Alloc>
404-
struct __is_cpp17_copy_insertable<
405-
_Alloc,
406-
__enable_if_t< !__is_default_allocator<_Alloc>::value &&
407-
__has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value > >
408-
: __is_cpp17_move_insertable<_Alloc> {};
383+
inline const bool __is_cpp17_copy_insertable_v =
384+
__is_cpp17_move_insertable_v<_Alloc> &&
385+
(is_copy_constructible<typename _Alloc::value_type>::value ||
386+
(!__is_std_allocator_v<_Alloc> &&
387+
__has_construct_v<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>));
409388

410389
#undef _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX
411390

libcxx/include/__memory/uninitialized_algorithms.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <__algorithm/unwrap_range.h>
1717
#include <__config>
1818
#include <__cstddef/size_t.h>
19+
#include <__fwd/memory.h>
1920
#include <__iterator/iterator_traits.h>
2021
#include <__iterator/reverse_iterator.h>
2122
#include <__memory/addressof.h>
@@ -32,7 +33,6 @@
3233
#include <__type_traits/is_trivially_constructible.h>
3334
#include <__type_traits/is_trivially_relocatable.h>
3435
#include <__type_traits/is_unbounded_array.h>
35-
#include <__type_traits/negation.h>
3636
#include <__type_traits/remove_const.h>
3737
#include <__type_traits/remove_extent.h>
3838
#include <__utility/exception_guard.h>
@@ -549,17 +549,17 @@ __uninitialized_allocator_copy_impl(_Alloc& __alloc, _Iter1 __first1, _Sent1 __l
549549
}
550550

551551
template <class _Alloc, class _Type>
552-
struct __allocator_has_trivial_copy_construct : _Not<__has_construct<_Alloc, _Type*, const _Type&> > {};
552+
inline const bool __allocator_has_trivial_copy_construct_v = !__has_construct_v<_Alloc, _Type*, const _Type&>;
553553

554554
template <class _Type>
555-
struct __allocator_has_trivial_copy_construct<allocator<_Type>, _Type> : true_type {};
555+
inline const bool __allocator_has_trivial_copy_construct_v<allocator<_Type>, _Type> = true;
556556

557557
template <class _Alloc,
558558
class _In,
559559
class _Out,
560560
__enable_if_t<is_trivially_copy_constructible<_In>::value && is_trivially_copy_assignable<_In>::value &&
561561
is_same<__remove_const_t<_In>, __remove_const_t<_Out> >::value &&
562-
__allocator_has_trivial_copy_construct<_Alloc, _In>::value,
562+
__allocator_has_trivial_copy_construct_v<_Alloc, _In>,
563563
int> = 0>
564564
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Out*
565565
__uninitialized_allocator_copy_impl(_Alloc&, _In* __first1, _In* __last1, _Out* __first2) {
@@ -585,16 +585,16 @@ __uninitialized_allocator_copy(_Alloc& __alloc, _Iter1 __first1, _Sent1 __last1,
585585
}
586586

587587
template <class _Alloc, class _Type>
588-
struct __allocator_has_trivial_move_construct : _Not<__has_construct<_Alloc, _Type*, _Type&&> > {};
588+
inline const bool __allocator_has_trivial_move_construct_v = !__has_construct_v<_Alloc, _Type*, _Type&&>;
589589

590590
template <class _Type>
591-
struct __allocator_has_trivial_move_construct<allocator<_Type>, _Type> : true_type {};
591+
inline const bool __allocator_has_trivial_move_construct_v<allocator<_Type>, _Type> = true;
592592

593593
template <class _Alloc, class _Tp>
594-
struct __allocator_has_trivial_destroy : _Not<__has_destroy<_Alloc, _Tp*> > {};
594+
inline const bool __allocator_has_trivial_destroy_v = !__has_destroy_v<_Alloc, _Tp*>;
595595

596596
template <class _Tp, class _Up>
597-
struct __allocator_has_trivial_destroy<allocator<_Tp>, _Up> : true_type {};
597+
inline const bool __allocator_has_trivial_destroy_v<allocator<_Tp>, _Up> = true;
598598

599599
// __uninitialized_allocator_relocate relocates the objects in [__first, __last) into __result.
600600
// Relocation means that the objects in [__first, __last) are placed into __result as-if by move-construct and destroy,
@@ -613,11 +613,11 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __uninitialized_allocat
613613
_Alloc& __alloc, _ContiguousIterator __first, _ContiguousIterator __last, _ContiguousIterator __result) {
614614
static_assert(__libcpp_is_contiguous_iterator<_ContiguousIterator>::value, "");
615615
using _ValueType = typename iterator_traits<_ContiguousIterator>::value_type;
616-
static_assert(__is_cpp17_move_insertable<_Alloc>::value,
617-
"The specified type does not meet the requirements of Cpp17MoveInsertable");
616+
static_assert(
617+
__is_cpp17_move_insertable_v<_Alloc>, "The specified type does not meet the requirements of Cpp17MoveInsertable");
618618
if (__libcpp_is_constant_evaluated() || !__libcpp_is_trivially_relocatable<_ValueType>::value ||
619-
!__allocator_has_trivial_move_construct<_Alloc, _ValueType>::value ||
620-
!__allocator_has_trivial_destroy<_Alloc, _ValueType>::value) {
619+
!__allocator_has_trivial_move_construct_v<_Alloc, _ValueType> ||
620+
!__allocator_has_trivial_destroy_v<_Alloc, _ValueType>) {
621621
auto __destruct_first = __result;
622622
auto __guard = std::__make_exception_guard(
623623
_AllocatorDestroyRangeReverse<_Alloc, _ContiguousIterator>(__alloc, __destruct_first, __result));

libcxx/include/__vector/container_traits.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct __container_traits<vector<_Tp, _Allocator> > {
3131
// there are no effects. Otherwise, if an exception is thrown by the move constructor of a non-Cpp17CopyInsertable T,
3232
// the effects are unspecified.
3333
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee =
34-
_Or<is_nothrow_move_constructible<_Tp>, __is_cpp17_copy_insertable<_Allocator> >::value;
34+
is_nothrow_move_constructible<_Tp>::value || __is_cpp17_copy_insertable_v<_Allocator>;
3535
};
3636

3737
_LIBCPP_END_NAMESPACE_STD

libcxx/include/deque

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2625,7 +2625,7 @@ struct __container_traits<deque<_Tp, _Allocator> > {
26252625
// either end, there are no effects. Otherwise, if an exception is thrown by the move constructor of a
26262626
// non-Cpp17CopyInsertable T, the effects are unspecified.
26272627
static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee =
2628-
_Or<is_nothrow_move_constructible<_Tp>, __is_cpp17_copy_insertable<_Allocator> >::value;
2628+
is_nothrow_move_constructible<_Tp>::value || __is_cpp17_copy_insertable_v<_Allocator>;
26292629
};
26302630

26312631
_LIBCPP_END_NAMESPACE_STD

0 commit comments

Comments
 (0)