From 54150e8257e462290e8f43f3a4153d12fb778216 Mon Sep 17 00:00:00 2001 From: Nikolas Klauser Date: Mon, 15 Aug 2022 14:09:49 +0200 Subject: [PATCH] [libc++][NFC] Refactor enable_ifs in vector Using the `enable_if_t<..., int> = 0` style has the benefit that it works in all cases and makes function declarations easier to read because the function arguments and return type and SFINAE are separated. Unifying the style also makes it easier for people not super familiar with SFINAE to make sense of the code. Reviewed By: Mordante, var-const, #libc, huixie90 Spies: huixie90, libcxx-commits Differential Revision: https://reviews.llvm.org/D131868 --- libcxx/include/vector | 234 +++++++++++++++++++------------------------------- 1 file changed, 89 insertions(+), 145 deletions(-) diff --git a/libcxx/include/vector b/libcxx/include/vector index 4404098..08b2d05 100644 --- a/libcxx/include/vector +++ b/libcxx/include/vector @@ -391,36 +391,31 @@ public: } } - template - _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI - vector(_InputIterator __first, - typename enable_if<__is_exactly_cpp17_input_iterator<_InputIterator>::value && - is_constructible< - value_type, - typename iterator_traits<_InputIterator>::reference>::value, - _InputIterator>::type __last); - template - _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI - vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a, - typename enable_if<__is_exactly_cpp17_input_iterator<_InputIterator>::value && - is_constructible< - value_type, - typename iterator_traits<_InputIterator>::reference>::value>::type* = 0); - template - _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI - vector(_ForwardIterator __first, - typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value && - is_constructible< - value_type, - typename iterator_traits<_ForwardIterator>::reference>::value, - _ForwardIterator>::type __last); - template - _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI - vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a, - typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value && - is_constructible< - value_type, - typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0); + template ::value && + is_constructible::reference>::value, + int> = 0> + _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(_InputIterator __first, _InputIterator __last); + template ::value && + is_constructible::reference>::value, + int> = 0> + _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI + vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a); + + template < + class _ForwardIterator, + __enable_if_t<__is_cpp17_forward_iterator<_ForwardIterator>::value && + is_constructible::reference>::value, + int> = 0> + _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector(_ForwardIterator __first, _ForwardIterator __last); + + template ::value && + is_constructible::reference>::value, + int> = 0> + _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI + vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a); _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~vector() @@ -466,26 +461,17 @@ public: vector& operator=(vector&& __x) _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value)); - template - _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI - typename enable_if <__is_exactly_cpp17_input_iterator<_InputIterator>::value && - is_constructible< - value_type, - typename iterator_traits<_InputIterator>::reference>::value, - void - >::type - assign(_InputIterator __first, _InputIterator __last); - template - _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI - typename enable_if - < - __is_cpp17_forward_iterator<_ForwardIterator>::value && - is_constructible< - value_type, - typename iterator_traits<_ForwardIterator>::reference>::value, - void - >::type - assign(_ForwardIterator __first, _ForwardIterator __last); + template ::value && + is_constructible::reference>::value, + int> = 0> + _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void assign(_InputIterator __first, _InputIterator __last); + template < + class _ForwardIterator, + __enable_if_t<__is_cpp17_forward_iterator<_ForwardIterator>::value && + is_constructible::reference>::value, + int> = 0> + _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void assign(_ForwardIterator __first, _ForwardIterator __last); _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const_reference __u); @@ -601,26 +587,20 @@ public: _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __position, size_type __n, const_reference __x); - template - _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI - typename enable_if <__is_exactly_cpp17_input_iterator<_InputIterator>::value && - is_constructible< - value_type, - typename iterator_traits<_InputIterator>::reference>::value, - iterator - >::type - insert(const_iterator __position, _InputIterator __first, _InputIterator __last); - template - _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI - typename enable_if - < - __is_cpp17_forward_iterator<_ForwardIterator>::value && - is_constructible< - value_type, - typename iterator_traits<_ForwardIterator>::reference>::value, - iterator - >::type - insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last); + template ::value && + is_constructible< value_type, typename iterator_traits<_InputIterator>::reference>::value, + int> = 0> + _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator + insert(const_iterator __position, _InputIterator __first, _InputIterator __last); + + template < + class _ForwardIterator, + __enable_if_t<__is_cpp17_forward_iterator<_ForwardIterator>::value && + is_constructible< value_type, typename iterator_traits<_ForwardIterator>::reference>::value, + int> = 0> + _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator + insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last); #ifndef _LIBCPP_CXX03_LANG _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI @@ -692,14 +672,11 @@ private: _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n); _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n, const_reference __x); - template - _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI - typename enable_if - < - __is_cpp17_forward_iterator<_ForwardIterator>::value, - void - >::type - __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n); + + template ::value, int> = 0> + _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void + __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n); + _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n); _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __append(size_type __n, const_reference __x); _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI @@ -1019,13 +996,8 @@ vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) } template -template -_LIBCPP_CONSTEXPR_SINCE_CXX20 -typename enable_if -< - __is_cpp17_forward_iterator<_ForwardIterator>::value, - void ->::type +template ::value, int> > +_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n) { _ConstructTransaction __tx(*this, __n); @@ -1112,14 +1084,11 @@ vector<_Tp, _Allocator>::vector(size_type __n, const value_type& __x) } template -template +template ::value && + is_constructible<_Tp, typename iterator_traits<_InputIterator>::reference>::value, + int> > _LIBCPP_CONSTEXPR_SINCE_CXX20 -vector<_Tp, _Allocator>::vector(_InputIterator __first, - typename enable_if<__is_exactly_cpp17_input_iterator<_InputIterator>::value && - is_constructible< - value_type, - typename iterator_traits<_InputIterator>::reference>::value, - _InputIterator>::type __last) +vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last) { std::__debug_db_insert_c(this); for (; __first != __last; ++__first) @@ -1127,13 +1096,11 @@ vector<_Tp, _Allocator>::vector(_InputIterator __first, } template -template +template ::value && + is_constructible<_Tp, typename iterator_traits<_InputIterator>::reference>::value, + int> > _LIBCPP_CONSTEXPR_SINCE_CXX20 -vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a, - typename enable_if<__is_exactly_cpp17_input_iterator<_InputIterator>::value && - is_constructible< - value_type, - typename iterator_traits<_InputIterator>::reference>::value>::type*) +vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a) : __end_cap_(nullptr, __a) { std::__debug_db_insert_c(this); @@ -1142,14 +1109,11 @@ vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, c } template -template +template ::value && + is_constructible<_Tp, typename iterator_traits<_ForwardIterator>::reference>::value, + int> > _LIBCPP_CONSTEXPR_SINCE_CXX20 -vector<_Tp, _Allocator>::vector(_ForwardIterator __first, - typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value && - is_constructible< - value_type, - typename iterator_traits<_ForwardIterator>::reference>::value, - _ForwardIterator>::type __last) +vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last) { std::__debug_db_insert_c(this); size_type __n = static_cast(std::distance(__first, __last)); @@ -1161,13 +1125,11 @@ vector<_Tp, _Allocator>::vector(_ForwardIterator __first, } template -template +template ::value && + is_constructible<_Tp, typename iterator_traits<_ForwardIterator>::reference>::value, + int> > _LIBCPP_CONSTEXPR_SINCE_CXX20 -vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a, - typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value && - is_constructible< - value_type, - typename iterator_traits<_ForwardIterator>::reference>::value>::type*) +vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a) : __end_cap_(nullptr, __a) { std::__debug_db_insert_c(this); @@ -1336,13 +1298,10 @@ vector<_Tp, _Allocator>::operator=(const vector& __x) } template -template -_LIBCPP_CONSTEXPR_SINCE_CXX20 typename enable_if <__is_exactly_cpp17_input_iterator<_InputIterator>::value && - is_constructible< - _Tp, - typename iterator_traits<_InputIterator>::reference>::value, - void ->::type +template ::value && + is_constructible<_Tp, typename iterator_traits<_InputIterator>::reference>::value, + int> > +_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last) { clear(); @@ -1351,16 +1310,10 @@ vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last) } template -template -_LIBCPP_CONSTEXPR_SINCE_CXX20 -typename enable_if -< - __is_cpp17_forward_iterator<_ForwardIterator>::value && - is_constructible< - _Tp, - typename iterator_traits<_ForwardIterator>::reference>::value, - void ->::type +template ::value && + is_constructible<_Tp, typename iterator_traits<_ForwardIterator>::reference>::value, + int> > +_LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last) { size_type __new_size = static_cast(std::distance(__first, __last)); @@ -1812,13 +1765,10 @@ vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_ } template -template -_LIBCPP_CONSTEXPR_SINCE_CXX20 typename enable_if <__is_exactly_cpp17_input_iterator<_InputIterator>::value && - is_constructible< - _Tp, - typename iterator_traits<_InputIterator>::reference>::value, - typename vector<_Tp, _Allocator>::iterator ->::type +template ::value && + is_constructible<_Tp, typename iterator_traits<_InputIterator>::reference>::value, + int> > +_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last) { _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(std::addressof(__position)) == this, @@ -1860,16 +1810,10 @@ vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __firs } template -template -_LIBCPP_CONSTEXPR_SINCE_CXX20 -typename enable_if -< - __is_cpp17_forward_iterator<_ForwardIterator>::value && - is_constructible< - _Tp, - typename iterator_traits<_ForwardIterator>::reference>::value, - typename vector<_Tp, _Allocator>::iterator ->::type +template ::value && + is_constructible<_Tp, typename iterator_traits<_ForwardIterator>::reference>::value, + int> > +_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last) { _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(std::addressof(__position)) == this, -- 2.7.4