Skip to content

Commit

Permalink
[libc++] Refactor more __enable_ifs to the canonical style (llvm#81457)
Browse files Browse the repository at this point in the history
This brings the code base closer to having only a single style of
`enable_if`s.
  • Loading branch information
philnik777 authored Feb 20, 2024
1 parent 91a3846 commit 76a2472
Show file tree
Hide file tree
Showing 30 changed files with 363 additions and 411 deletions.
45 changes: 24 additions & 21 deletions libcxx/include/__algorithm/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,18 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1> __searc
}
}

template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Pred, class _Proj1, class _Proj2>
template <class _Iter1,
class _Sent1,
class _Iter2,
class _Sent2,
class _Pred,
class _Proj1,
class _Proj2,
__enable_if_t<__has_random_access_iterator_category<_Iter1>::value &&
__has_random_access_iterator_category<_Iter2>::value,
int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1> __search_impl(
_Iter1 __first1,
_Sent1 __last1,
_Iter2 __first2,
_Sent2 __last2,
_Pred& __pred,
_Proj1& __proj1,
_Proj2& __proj2,
__enable_if_t<__has_random_access_iterator_category<_Iter1>::value &&
__has_random_access_iterator_category<_Iter2>::value>* = nullptr) {
_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2, _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
auto __size2 = __last2 - __first2;
if (__size2 == 0)
return std::make_pair(__first1, __first1);
Expand All @@ -141,18 +142,20 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1> __searc
__first1, __last1, __first2, __last2, __pred, __proj1, __proj2, __size1, __size2);
}

template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Pred, class _Proj1, class _Proj2>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1> __search_impl(
_Iter1 __first1,
_Sent1 __last1,
_Iter2 __first2,
_Sent2 __last2,
_Pred& __pred,
_Proj1& __proj1,
_Proj2& __proj2,
template <
class _Iter1,
class _Sent1,
class _Iter2,
class _Sent2,
class _Pred,
class _Proj1,
class _Proj2,
__enable_if_t<__has_forward_iterator_category<_Iter1>::value && __has_forward_iterator_category<_Iter2>::value &&
!(__has_random_access_iterator_category<_Iter1>::value &&
__has_random_access_iterator_category<_Iter2>::value)>* = nullptr) {
!(__has_random_access_iterator_category<_Iter1>::value &&
__has_random_access_iterator_category<_Iter2>::value),
int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1> __search_impl(
_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2, _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
return std::__search_forward_impl<_ClassicAlgPolicy>(__first1, __last1, __first2, __last2, __pred, __proj1, __proj2);
}

Expand Down
39 changes: 20 additions & 19 deletions libcxx/include/__algorithm/search_n.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,29 +108,30 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 std::pair<_Iter, _Iter> __se
}
}

template <class _Iter, class _Sent, class _DiffT, class _Type, class _Pred, class _Proj>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter> __search_n_impl(
_Iter __first,
_Sent __last,
_DiffT __count,
const _Type& __value,
_Pred& __pred,
_Proj& __proj,
__enable_if_t<__has_random_access_iterator_category<_Iter>::value>* = nullptr) {
template <class _Iter,
class _Sent,
class _DiffT,
class _Type,
class _Pred,
class _Proj,
__enable_if_t<__has_random_access_iterator_category<_Iter>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter>
__search_n_impl(_Iter __first, _Sent __last, _DiffT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
return std::__search_n_random_access_impl<_ClassicAlgPolicy>(
__first, __last, __count, __value, __pred, __proj, __last - __first);
}

template <class _Iter1, class _Sent1, class _DiffT, class _Type, class _Pred, class _Proj>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1> __search_n_impl(
_Iter1 __first,
_Sent1 __last,
_DiffT __count,
const _Type& __value,
_Pred& __pred,
_Proj& __proj,
__enable_if_t<__has_forward_iterator_category<_Iter1>::value &&
!__has_random_access_iterator_category<_Iter1>::value>* = nullptr) {
template <class _Iter1,
class _Sent1,
class _DiffT,
class _Type,
class _Pred,
class _Proj,
__enable_if_t<__has_forward_iterator_category<_Iter1>::value &&
!__has_random_access_iterator_category<_Iter1>::value,
int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter1, _Iter1>
__search_n_impl(_Iter1 __first, _Sent1 __last, _DiffT __count, const _Type& __value, _Pred& __pred, _Proj& __proj) {
return std::__search_n_forward_impl<_ClassicAlgPolicy>(__first, __last, __count, __value, __pred, __proj);
}

Expand Down
7 changes: 4 additions & 3 deletions libcxx/include/__functional/reference_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <__memory/addressof.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/remove_cvref.h>
#include <__type_traits/void_t.h>
#include <__utility/declval.h>
#include <__utility/forward.h>

Expand All @@ -38,9 +39,9 @@ class _LIBCPP_TEMPLATE_VIS reference_wrapper : public __weak_result_type<_Tp> {
static void __fun(_Tp&&) = delete;

public:
template <
class _Up,
class = __enable_if_t<!__is_same_uncvref<_Up, reference_wrapper>::value, decltype(__fun(std::declval<_Up>())) > >
template <class _Up,
class = __void_t<decltype(__fun(std::declval<_Up>()))>,
__enable_if_t<!__is_same_uncvref<_Up, reference_wrapper>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper(_Up&& __u)
_NOEXCEPT_(noexcept(__fun(std::declval<_Up>()))) {
type& __f = static_cast<_Up&&>(__u);
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__hash_table
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ public:
return __emplace_unique_key_args(_NodeTypes::__get_key(__x), std::move(__x));
}

template <class _Pp, class = __enable_if_t<!__is_same_uncvref<_Pp, __container_value_type>::value> >
template <class _Pp, __enable_if_t<!__is_same_uncvref<_Pp, __container_value_type>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __insert_unique(_Pp&& __x) {
return __emplace_unique(std::forward<_Pp>(__x));
}
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__iterator/advance.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ __advance(_RandIter& __i, typename iterator_traits<_RandIter>::difference_type _
template < class _InputIter,
class _Distance,
class _IntegralDistance = decltype(std::__convert_to_integral(std::declval<_Distance>())),
class = __enable_if_t<is_integral<_IntegralDistance>::value> >
__enable_if_t<is_integral<_IntegralDistance>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 void advance(_InputIter& __i, _Distance __orig_n) {
typedef typename iterator_traits<_InputIter>::difference_type _Difference;
_Difference __n = static_cast<_Difference>(std::__convert_to_integral(__orig_n));
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__iterator/bounded_iter.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct __bounded_iter {
_LIBCPP_HIDE_FROM_ABI __bounded_iter(__bounded_iter const&) = default;
_LIBCPP_HIDE_FROM_ABI __bounded_iter(__bounded_iter&&) = default;

template <class _OtherIterator, class = __enable_if_t< is_convertible<_OtherIterator, _Iterator>::value > >
template <class _OtherIterator, __enable_if_t< is_convertible<_OtherIterator, _Iterator>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __bounded_iter(__bounded_iter<_OtherIterator> const& __other) _NOEXCEPT
: __current_(__other.__current_),
__begin_(__other.__begin_),
Expand Down
8 changes: 4 additions & 4 deletions libcxx/include/__iterator/move_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ class _LIBCPP_TEMPLATE_VIS move_iterator
#else
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 move_iterator() : __current_() {}

template <class _Up,
class = __enable_if_t< !is_same<_Up, _Iter>::value && is_convertible<const _Up&, _Iter>::value > >
template <class _Up, __enable_if_t< !is_same<_Up, _Iter>::value && is_convertible<const _Up&, _Iter>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 move_iterator(const move_iterator<_Up>& __u)
: __current_(__u.base()) {}

template <class _Up,
class = __enable_if_t< !is_same<_Up, _Iter>::value && is_convertible<const _Up&, _Iter>::value &&
is_assignable<_Iter&, const _Up&>::value > >
__enable_if_t< !is_same<_Up, _Iter>::value && is_convertible<const _Up&, _Iter>::value &&
is_assignable<_Iter&, const _Up&>::value,
int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 move_iterator& operator=(const move_iterator<_Up>& __u) {
__current_ = __u.base();
return *this;
Expand Down
16 changes: 8 additions & 8 deletions libcxx/include/__iterator/reverse_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ class _LIBCPP_TEMPLATE_VIS reverse_iterator

_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 explicit reverse_iterator(_Iter __x) : __t_(__x), current(__x) {}

template <class _Up,
class = __enable_if_t< !is_same<_Up, _Iter>::value && is_convertible<_Up const&, _Iter>::value > >
template <class _Up, __enable_if_t<!is_same<_Up, _Iter>::value && is_convertible<_Up const&, _Iter>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 reverse_iterator(const reverse_iterator<_Up>& __u)
: __t_(__u.base()), current(__u.base()) {}

template <class _Up,
class = __enable_if_t< !is_same<_Up, _Iter>::value && is_convertible<_Up const&, _Iter>::value &&
is_assignable<_Iter&, _Up const&>::value > >
__enable_if_t<!is_same<_Up, _Iter>::value && is_convertible<_Up const&, _Iter>::value &&
is_assignable<_Iter&, _Up const&>::value,
int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 reverse_iterator& operator=(const reverse_iterator<_Up>& __u) {
__t_ = current = __u.base();
return *this;
Expand All @@ -113,14 +113,14 @@ class _LIBCPP_TEMPLATE_VIS reverse_iterator

_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 explicit reverse_iterator(_Iter __x) : current(__x) {}

template <class _Up,
class = __enable_if_t< !is_same<_Up, _Iter>::value && is_convertible<_Up const&, _Iter>::value > >
template <class _Up, __enable_if_t<!is_same<_Up, _Iter>::value && is_convertible<_Up const&, _Iter>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 reverse_iterator(const reverse_iterator<_Up>& __u)
: current(__u.base()) {}

template <class _Up,
class = __enable_if_t< !is_same<_Up, _Iter>::value && is_convertible<_Up const&, _Iter>::value &&
is_assignable<_Iter&, _Up const&>::value > >
__enable_if_t<!is_same<_Up, _Iter>::value && is_convertible<_Up const&, _Iter>::value &&
is_assignable<_Iter&, _Up const&>::value,
int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 reverse_iterator& operator=(const reverse_iterator<_Up>& __u) {
current = __u.base();
return *this;
Expand Down
30 changes: 15 additions & 15 deletions libcxx/include/__memory/allocator_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,16 +281,16 @@ struct _LIBCPP_TEMPLATE_VIS allocator_traits {
return __a.allocate(__n);
}

template <class _Ap = _Alloc, class = __enable_if_t<__has_allocate_hint<_Ap, size_type, const_void_pointer>::value> >
template <class _Ap = _Alloc, __enable_if_t<__has_allocate_hint<_Ap, size_type, const_void_pointer>::value, int> = 0>
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer
allocate(allocator_type& __a, size_type __n, const_void_pointer __hint) {
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
return __a.allocate(__n, __hint);
_LIBCPP_SUPPRESS_DEPRECATED_POP
}
template <class _Ap = _Alloc,
class = void,
class = __enable_if_t<!__has_allocate_hint<_Ap, size_type, const_void_pointer>::value> >
template <class _Ap = _Alloc,
class = void,
__enable_if_t<!__has_allocate_hint<_Ap, size_type, const_void_pointer>::value, int> = 0>
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer
allocate(allocator_type& __a, size_type __n, const_void_pointer) {
return __a.allocate(__n);
Expand All @@ -313,7 +313,7 @@ struct _LIBCPP_TEMPLATE_VIS allocator_traits {
__a.deallocate(__p, __n);
}

template <class _Tp, class... _Args, class = __enable_if_t<__has_construct<allocator_type, _Tp*, _Args...>::value> >
template <class _Tp, class... _Args, __enable_if_t<__has_construct<allocator_type, _Tp*, _Args...>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void
construct(allocator_type& __a, _Tp* __p, _Args&&... __args) {
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
Expand All @@ -322,43 +322,43 @@ struct _LIBCPP_TEMPLATE_VIS allocator_traits {
}
template <class _Tp,
class... _Args,
class = void,
class = __enable_if_t<!__has_construct<allocator_type, _Tp*, _Args...>::value> >
class = void,
__enable_if_t<!__has_construct<allocator_type, _Tp*, _Args...>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void
construct(allocator_type&, _Tp* __p, _Args&&... __args) {
std::__construct_at(__p, std::forward<_Args>(__args)...);
}

template <class _Tp, class = __enable_if_t<__has_destroy<allocator_type, _Tp*>::value> >
template <class _Tp, __enable_if_t<__has_destroy<allocator_type, _Tp*>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void destroy(allocator_type& __a, _Tp* __p) {
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
__a.destroy(__p);
_LIBCPP_SUPPRESS_DEPRECATED_POP
}
template <class _Tp, class = void, class = __enable_if_t<!__has_destroy<allocator_type, _Tp*>::value> >
template <class _Tp, class = void, __enable_if_t<!__has_destroy<allocator_type, _Tp*>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void destroy(allocator_type&, _Tp* __p) {
std::__destroy_at(__p);
}

template <class _Ap = _Alloc, class = __enable_if_t<__has_max_size<const _Ap>::value> >
template <class _Ap = _Alloc, __enable_if_t<__has_max_size<const _Ap>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static size_type max_size(const allocator_type& __a) _NOEXCEPT {
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
return __a.max_size();
_LIBCPP_SUPPRESS_DEPRECATED_POP
}
template <class _Ap = _Alloc, class = void, class = __enable_if_t<!__has_max_size<const _Ap>::value> >
template <class _Ap = _Alloc, class = void, __enable_if_t<!__has_max_size<const _Ap>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static size_type max_size(const allocator_type&) _NOEXCEPT {
return numeric_limits<size_type>::max() / sizeof(value_type);
}

template <class _Ap = _Alloc, class = __enable_if_t<__has_select_on_container_copy_construction<const _Ap>::value> >
template <class _Ap = _Alloc, __enable_if_t<__has_select_on_container_copy_construction<const _Ap>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static allocator_type
select_on_container_copy_construction(const allocator_type& __a) {
return __a.select_on_container_copy_construction();
}
template <class _Ap = _Alloc,
class = void,
class = __enable_if_t<!__has_select_on_container_copy_construction<const _Ap>::value> >
template <class _Ap = _Alloc,
class = void,
__enable_if_t<!__has_select_on_container_copy_construction<const _Ap>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static allocator_type
select_on_container_copy_construction(const allocator_type& __a) {
return __a;
Expand Down
11 changes: 6 additions & 5 deletions libcxx/include/__memory/compressed_pair.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct __compressed_pair_elem {
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__default_init_tag) {}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__value_init_tag) : __value_() {}

template <class _Up, class = __enable_if_t<!is_same<__compressed_pair_elem, __decay_t<_Up> >::value> >
template <class _Up, __enable_if_t<!is_same<__compressed_pair_elem, __decay_t<_Up> >::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(_Up&& __u)
: __value_(std::forward<_Up>(__u)) {}

Expand Down Expand Up @@ -77,7 +77,7 @@ struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__default_init_tag) {}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__value_init_tag) : __value_type() {}

template <class _Up, class = __enable_if_t<!is_same<__compressed_pair_elem, __decay_t<_Up> >::value> >
template <class _Up, __enable_if_t<!is_same<__compressed_pair_elem, __decay_t<_Up> >::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(_Up&& __u)
: __value_type(std::forward<_Up>(__u)) {}

Expand Down Expand Up @@ -107,9 +107,10 @@ class __compressed_pair : private __compressed_pair_elem<_T1, 0>, private __comp
using _Base1 _LIBCPP_NODEBUG = __compressed_pair_elem<_T1, 0>;
using _Base2 _LIBCPP_NODEBUG = __compressed_pair_elem<_T2, 1>;

template <bool _Dummy = true,
class = __enable_if_t< __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
__dependent_type<is_default_constructible<_T2>, _Dummy>::value > >
template <bool _Dummy = true,
__enable_if_t< __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
__dependent_type<is_default_constructible<_T2>, _Dummy>::value,
int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair()
: _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}

Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__memory/pointer_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ struct _IsFancyPointer {
};

// enable_if is needed here to avoid instantiating checks for fancy pointers on raw pointers
template <class _Pointer, class = __enable_if_t< _And<is_class<_Pointer>, _IsFancyPointer<_Pointer> >::value > >
template <class _Pointer, __enable_if_t< _And<is_class<_Pointer>, _IsFancyPointer<_Pointer> >::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
__decay_t<decltype(__to_address_helper<_Pointer>::__call(std::declval<const _Pointer&>()))>
__to_address(const _Pointer& __p) _NOEXCEPT {
Expand Down
Loading

0 comments on commit 76a2472

Please sign in to comment.