Skip to content

Commit 30213e9

Browse files
authored
[libc++] Use enable_if_t to constrain make_unique{,_for_overwrite} (#95044)
This improves the diagnostics a bit by using `enable_if_t` which clang is aware of, instead of a custom SFINAE class, as well as writing the functions in a more canonical style. As a drive-by this also makes `__is_{,un}bounded_array` variable templates instead of class templates.
1 parent 88823d0 commit 30213e9

File tree

5 files changed

+21
-38
lines changed

5 files changed

+21
-38
lines changed

libcxx/include/__memory/shared_ptr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ struct __unbounded_array_control_block<_Tp[], _Alloc> : __shared_weak_count {
957957
template <class _Array, class _Alloc, class... _Arg>
958958
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Array>
959959
__allocate_shared_unbounded_array(const _Alloc& __a, size_t __n, _Arg&&... __arg) {
960-
static_assert(__libcpp_is_unbounded_array<_Array>::value);
960+
static_assert(__is_unbounded_array_v<_Array>);
961961
// We compute the number of bytes necessary to hold the control block and the
962962
// array elements. Then, we allocate an array of properly-aligned dummy structs
963963
// large enough to hold the control block and array. This allows shifting the
@@ -1034,7 +1034,7 @@ struct __bounded_array_control_block<_Tp[_Count], _Alloc> : __shared_weak_count
10341034

10351035
template <class _Array, class _Alloc, class... _Arg>
10361036
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Array> __allocate_shared_bounded_array(const _Alloc& __a, _Arg&&... __arg) {
1037-
static_assert(__libcpp_is_bounded_array<_Array>::value);
1037+
static_assert(__is_bounded_array_v<_Array>);
10381038
using _ControlBlock = __bounded_array_control_block<_Array, _Alloc>;
10391039
using _ControlBlockAlloc = __allocator_traits_rebind_t<_Alloc, _ControlBlock>;
10401040

libcxx/include/__memory/uninitialized_algorithms.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ __allocator_destroy_multidimensional(_Alloc& __alloc, _BidirIter __first, _Bidir
375375
return;
376376

377377
if constexpr (is_array_v<_ValueType>) {
378-
static_assert(!__libcpp_is_unbounded_array<_ValueType>::value,
378+
static_assert(!__is_unbounded_array_v<_ValueType>,
379379
"arrays of unbounded arrays don't exist, but if they did we would mess up here");
380380

381381
using _Element = remove_extent_t<_ValueType>;

libcxx/include/__memory/unique_ptr.h

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <__type_traits/integral_constant.h>
3333
#include <__type_traits/is_array.h>
3434
#include <__type_traits/is_assignable.h>
35+
#include <__type_traits/is_bounded_array.h>
3536
#include <__type_traits/is_constant_evaluated.h>
3637
#include <__type_traits/is_constructible.h>
3738
#include <__type_traits/is_convertible.h>
@@ -41,6 +42,7 @@
4142
#include <__type_traits/is_same.h>
4243
#include <__type_traits/is_swappable.h>
4344
#include <__type_traits/is_trivially_relocatable.h>
45+
#include <__type_traits/is_unbounded_array.h>
4446
#include <__type_traits/is_void.h>
4547
#include <__type_traits/remove_extent.h>
4648
#include <__type_traits/type_identity.h>
@@ -758,55 +760,36 @@ operator<=>(const unique_ptr<_T1, _D1>& __x, nullptr_t) {
758760

759761
#if _LIBCPP_STD_VER >= 14
760762

761-
template <class _Tp>
762-
struct __unique_if {
763-
typedef unique_ptr<_Tp> __unique_single;
764-
};
765-
766-
template <class _Tp>
767-
struct __unique_if<_Tp[]> {
768-
typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
769-
};
770-
771-
template <class _Tp, size_t _Np>
772-
struct __unique_if<_Tp[_Np]> {
773-
typedef void __unique_array_known_bound;
774-
};
775-
776-
template <class _Tp, class... _Args>
777-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename __unique_if<_Tp>::__unique_single
778-
make_unique(_Args&&... __args) {
763+
template <class _Tp, class... _Args, enable_if_t<!is_array<_Tp>::value, int> = 0>
764+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr<_Tp> make_unique(_Args&&... __args) {
779765
return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...));
780766
}
781767

782-
template <class _Tp>
783-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename __unique_if<_Tp>::__unique_array_unknown_bound
784-
make_unique(size_t __n) {
768+
template <class _Tp, enable_if_t<__is_unbounded_array_v<_Tp>, int> = 0>
769+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr<_Tp> make_unique(size_t __n) {
785770
typedef __remove_extent_t<_Tp> _Up;
786771
return unique_ptr<_Tp>(__private_constructor_tag(), new _Up[__n](), __n);
787772
}
788773

789-
template <class _Tp, class... _Args>
790-
typename __unique_if<_Tp>::__unique_array_known_bound make_unique(_Args&&...) = delete;
774+
template <class _Tp, class... _Args, enable_if_t<__is_bounded_array_v<_Tp>, int> = 0>
775+
void make_unique(_Args&&...) = delete;
791776

792777
#endif // _LIBCPP_STD_VER >= 14
793778

794779
#if _LIBCPP_STD_VER >= 20
795780

796-
template <class _Tp>
797-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename __unique_if<_Tp>::__unique_single
798-
make_unique_for_overwrite() {
781+
template <class _Tp, enable_if_t<!is_array_v<_Tp>, int> = 0>
782+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr<_Tp> make_unique_for_overwrite() {
799783
return unique_ptr<_Tp>(new _Tp);
800784
}
801785

802-
template <class _Tp>
803-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename __unique_if<_Tp>::__unique_array_unknown_bound
804-
make_unique_for_overwrite(size_t __n) {
786+
template <class _Tp, enable_if_t<is_unbounded_array_v<_Tp>, int> = 0>
787+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr<_Tp> make_unique_for_overwrite(size_t __n) {
805788
return unique_ptr<_Tp>(__private_constructor_tag(), new __remove_extent_t<_Tp>[__n], __n);
806789
}
807790

808-
template <class _Tp, class... _Args>
809-
typename __unique_if<_Tp>::__unique_array_known_bound make_unique_for_overwrite(_Args&&...) = delete;
791+
template <class _Tp, class... _Args, enable_if_t<is_bounded_array_v<_Tp>, int> = 0>
792+
void make_unique_for_overwrite(_Args&&...) = delete;
810793

811794
#endif // _LIBCPP_STD_VER >= 20
812795

libcxx/include/__type_traits/is_bounded_array.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
_LIBCPP_BEGIN_NAMESPACE_STD
2121

2222
template <class>
23-
struct _LIBCPP_TEMPLATE_VIS __libcpp_is_bounded_array : false_type {};
23+
inline const bool __is_bounded_array_v = false;
2424
template <class _Tp, size_t _Np>
25-
struct _LIBCPP_TEMPLATE_VIS __libcpp_is_bounded_array<_Tp[_Np]> : true_type {};
25+
inline const bool __is_bounded_array_v<_Tp[_Np]> = true;
2626

2727
#if _LIBCPP_STD_VER >= 20
2828

libcxx/include/__type_traits/is_unbounded_array.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
_LIBCPP_BEGIN_NAMESPACE_STD
2020

2121
template <class>
22-
struct _LIBCPP_TEMPLATE_VIS __libcpp_is_unbounded_array : false_type {};
22+
inline const bool __is_unbounded_array_v = false;
2323
template <class _Tp>
24-
struct _LIBCPP_TEMPLATE_VIS __libcpp_is_unbounded_array<_Tp[]> : true_type {};
24+
inline const bool __is_unbounded_array_v<_Tp[]> = true;
2525

2626
#if _LIBCPP_STD_VER >= 20
2727

0 commit comments

Comments
 (0)