[libc++] Add __decay_t and use it instead of decay<>::type

This avoids instantiating lots of types.

Reviewed By: ldionne, #libc

Spies: libcxx-commits

Differential Revision: https://reviews.llvm.org/D146984
This commit is contained in:
Nikolas Klauser 2023-03-27 18:22:23 +02:00
parent 00701d32b7
commit 173476ea04
18 changed files with 60 additions and 52 deletions

View File

@ -57,8 +57,8 @@ struct _ProjectedPred {
template <class _Pred,
class _Proj,
__enable_if_t<!(!is_member_pointer<typename decay<_Pred>::type>::value &&
__is_identity<typename decay<_Proj>::type>::value),
__enable_if_t<!(!is_member_pointer<__decay_t<_Pred> >::value &&
__is_identity<__decay_t<_Proj> >::value),
int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ProjectedPred<_Pred, _Proj>
__make_projected(_Pred& __pred, _Proj& __proj) {
@ -70,8 +70,8 @@ __make_projected(_Pred& __pred, _Proj& __proj) {
// the call stack when the comparator is invoked, even in an unoptimized build.
template <class _Pred,
class _Proj,
__enable_if_t<!is_member_pointer<typename decay<_Pred>::type>::value &&
__is_identity<typename decay<_Proj>::type>::value,
__enable_if_t<!is_member_pointer<__decay_t<_Pred> >::value &&
__is_identity<__decay_t<_Proj> >::value,
int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Pred& __make_projected(_Pred& __pred, _Proj&) {
return __pred;

View File

@ -65,7 +65,7 @@ template <class _Atp, class _Fn>
_LIBCPP_AVAILABILITY_SYNC
_LIBCPP_HIDE_FROM_ABI bool __cxx_atomic_wait(_Atp* __a, _Fn && __test_fn)
{
__libcpp_atomic_wait_backoff_impl<_Atp, typename decay<_Fn>::type> __backoff_fn = {__a, __test_fn};
__libcpp_atomic_wait_backoff_impl<_Atp, __decay_t<_Fn> > __backoff_fn = {__a, __test_fn};
return std::__libcpp_thread_poll_with_backoff(__test_fn, __backoff_fn);
}

View File

@ -67,7 +67,7 @@ struct __throw_with_nested<_Tp, _Up, false> {
template <class _Tp>
_LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI void throw_with_nested(_Tp&& __t) {
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
typedef typename decay<_Tp>::type _Up;
using _Up = __decay_t<_Tp>;
static_assert(is_copy_constructible<_Up>::value, "type thrown must be CopyConstructible");
__throw_with_nested<_Tp,
_Up,

View File

@ -144,7 +144,7 @@ struct __is_pathable_string<
}
};
template <class _Source, class _DS = typename decay<_Source>::type,
template <class _Source, class _DS = __decay_t<_Source>,
class _UnqualPtrType =
__remove_const_t<__remove_pointer_t<_DS> >,
bool _IsCharPtr = is_pointer<_DS>::value&&

View File

@ -263,11 +263,11 @@ __apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
}
template<class _Fp, class ..._BoundArgs>
class __bind : public __weak_result_type<typename decay<_Fp>::type>
class __bind : public __weak_result_type<__decay_t<_Fp> >
{
protected:
typedef typename decay<_Fp>::type _Fd;
typedef tuple<typename decay<_BoundArgs>::type...> _Td;
using _Fd = __decay_t<_Fp>;
typedef tuple<__decay_t<_BoundArgs>...> _Td;
private:
_Fd __f_;
_Td __bound_args_;

View File

@ -430,7 +430,7 @@ template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
}
template <class _Fp,
class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type>
class = typename enable_if<!is_same<__decay_t<_Fp>, __value_func>::value>::type>
_LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
: __value_func(_VSTD::forward<_Fp>(__f), allocator<_Fp>()) {}
@ -773,7 +773,7 @@ template <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
}
}
template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type>
template <class _Fp, class = typename enable_if<!is_same<__decay_t<_Fp>, __policy_func>::value>::type>
_LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
: __policy_(__policy::__create_empty()) {
typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
@ -1029,7 +1029,7 @@ public:
function& operator=(const function&);
function& operator=(function&&) _NOEXCEPT;
function& operator=(nullptr_t) _NOEXCEPT;
template<class _Fp, class = _EnableIfLValueCallable<typename decay<_Fp>::type>>
template<class _Fp, class = _EnableIfLValueCallable<__decay_t<_Fp>>>
function& operator=(_Fp&&);
~function();

View File

@ -266,8 +266,8 @@ struct __member_pointer_class_type<_Ret _ClassType::*> {
};
template <class _Fp, class _A0,
class _DecayFp = typename decay<_Fp>::type,
class _DecayA0 = typename decay<_A0>::type,
class _DecayFp = __decay_t<_Fp>,
class _DecayA0 = __decay_t<_A0>,
class _ClassT = typename __member_pointer_class_type<_DecayFp>::type>
using __enable_if_bullet1 = typename enable_if
<
@ -276,8 +276,8 @@ using __enable_if_bullet1 = typename enable_if
>::type;
template <class _Fp, class _A0,
class _DecayFp = typename decay<_Fp>::type,
class _DecayA0 = typename decay<_A0>::type>
class _DecayFp = __decay_t<_Fp>,
class _DecayA0 = __decay_t<_A0> >
using __enable_if_bullet2 = typename enable_if
<
is_member_function_pointer<_DecayFp>::value
@ -285,8 +285,8 @@ using __enable_if_bullet2 = typename enable_if
>::type;
template <class _Fp, class _A0,
class _DecayFp = typename decay<_Fp>::type,
class _DecayA0 = typename decay<_A0>::type,
class _DecayFp = __decay_t<_Fp>,
class _DecayA0 = __decay_t<_A0>,
class _ClassT = typename __member_pointer_class_type<_DecayFp>::type>
using __enable_if_bullet3 = typename enable_if
<
@ -296,8 +296,8 @@ using __enable_if_bullet3 = typename enable_if
>::type;
template <class _Fp, class _A0,
class _DecayFp = typename decay<_Fp>::type,
class _DecayA0 = typename decay<_A0>::type,
class _DecayFp = __decay_t<_Fp>,
class _DecayA0 = __decay_t<_A0>,
class _ClassT = typename __member_pointer_class_type<_DecayFp>::type>
using __enable_if_bullet4 = typename enable_if
<
@ -306,8 +306,8 @@ using __enable_if_bullet4 = typename enable_if
>::type;
template <class _Fp, class _A0,
class _DecayFp = typename decay<_Fp>::type,
class _DecayA0 = typename decay<_A0>::type>
class _DecayFp = __decay_t<_Fp>,
class _DecayA0 = __decay_t<_A0> >
using __enable_if_bullet5 = typename enable_if
<
is_member_object_pointer<_DecayFp>::value
@ -315,8 +315,8 @@ using __enable_if_bullet5 = typename enable_if
>::type;
template <class _Fp, class _A0,
class _DecayFp = typename decay<_Fp>::type,
class _DecayA0 = typename decay<_A0>::type,
class _DecayFp = __decay_t<_Fp>,
class _DecayA0 = __decay_t<_A0>,
class _ClassT = typename __member_pointer_class_type<_DecayFp>::type>
using __enable_if_bullet6 = typename enable_if
<

View File

@ -38,7 +38,7 @@ template <class _Tp>
using unwrap_reference_t = typename unwrap_reference<_Tp>::type;
template <class _Tp>
struct unwrap_ref_decay : unwrap_reference<typename decay<_Tp>::type> { };
struct unwrap_ref_decay : unwrap_reference<__decay_t<_Tp> > { };
template <class _Tp>
using unwrap_ref_decay_t = typename unwrap_ref_decay<_Tp>::type;
@ -49,7 +49,7 @@ struct __unwrap_ref_decay
#if _LIBCPP_STD_VER >= 20
: unwrap_ref_decay<_Tp>
#else
: __unwrap_reference<typename decay<_Tp>::type>
: __unwrap_reference<__decay_t<_Tp> >
#endif
{ };

View File

@ -46,7 +46,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, typename decay<_Up>::type>::value> >
template <class _Up, class = __enable_if_t<!is_same<__compressed_pair_elem, __decay_t<_Up> >::value> >
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
explicit __compressed_pair_elem(_Up&& __u) : __value_(std::forward<_Up>(__u)) {}
@ -75,7 +75,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, typename decay<_Up>::type>::value> >
template <class _Up, class = __enable_if_t<!is_same<__compressed_pair_elem, __decay_t<_Up> >::value> >
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
explicit __compressed_pair_elem(_Up&& __u) : __value_type(std::forward<_Up>(__u)) {}

View File

@ -200,7 +200,7 @@ template <class _Pointer, class = __enable_if_t<
_And<is_class<_Pointer>, _IsFancyPointer<_Pointer> >::value
> >
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
typename decay<decltype(__to_address_helper<_Pointer>::__call(std::declval<const _Pointer&>()))>::type
__decay_t<decltype(__to_address_helper<_Pointer>::__call(std::declval<const _Pointer&>()))>
__to_address(const _Pointer& __p) _NOEXCEPT {
return __to_address_helper<_Pointer>::__call(__p);
}

View File

@ -49,9 +49,9 @@ struct __common_type2_imp {};
template <class _Tp, class _Up>
struct __common_type2_imp<_Tp, _Up, __void_t<decltype(true ? std::declval<_Tp>() : std::declval<_Up>())> >
{
typedef _LIBCPP_NODEBUG typename decay<decltype(
typedef _LIBCPP_NODEBUG __decay_t<decltype(
true ? std::declval<_Tp>() : std::declval<_Up>()
)>::type type;
)> type;
};
template <class, class = void>
@ -109,9 +109,9 @@ struct _LIBCPP_TEMPLATE_VIS common_type<_Tp>
template <class _Tp, class _Up>
struct _LIBCPP_TEMPLATE_VIS common_type<_Tp, _Up>
: conditional<
_IsSame<_Tp, typename decay<_Tp>::type>::value && _IsSame<_Up, typename decay<_Up>::type>::value,
_IsSame<_Tp, __decay_t<_Tp> >::value && _IsSame<_Up, __decay_t<_Up> >::value,
__common_type2_imp<_Tp, _Up>,
common_type<typename decay<_Tp>::type, typename decay<_Up>::type>
common_type<__decay_t<_Tp>, __decay_t<_Up> >
>::type
{};

View File

@ -26,10 +26,14 @@
_LIBCPP_BEGIN_NAMESPACE_STD
#if __has_builtin(__decay)
template <class _Tp>
using __decay_t _LIBCPP_NODEBUG = __decay(_Tp);
template <class _Tp>
struct decay {
using type _LIBCPP_NODEBUG = __decay(_Tp);
using type _LIBCPP_NODEBUG = __decay_t<_Tp>;
};
#else
template <class _Up, bool>
struct __decay {
@ -60,10 +64,13 @@ private:
public:
typedef _LIBCPP_NODEBUG typename __decay<_Up, __libcpp_is_referenceable<_Up>::value>::type type;
};
template <class _Tp>
using __decay_t = typename decay<_Tp>::type;
#endif // __has_builtin(__decay)
#if _LIBCPP_STD_VER >= 14
template <class _Tp> using decay_t = typename decay<_Tp>::type;
template <class _Tp> using decay_t = __decay_t<_Tp>;
#endif
_LIBCPP_END_NAMESPACE_STD

View File

@ -17,6 +17,6 @@
# pragma GCC system_header
#endif
#define _LIBCPP_AUTO_CAST(expr) static_cast<typename decay<decltype((expr))>::type>(expr)
#define _LIBCPP_AUTO_CAST(expr) static_cast<::std::__decay_t<decltype((expr))> >(expr)
#endif // _LIBCPP___UTILITY_AUTO_CAST_H

View File

@ -22,6 +22,7 @@
#include <__type_traits/common_reference.h>
#include <__type_traits/common_type.h>
#include <__type_traits/conditional.h>
#include <__type_traits/decay.h>
#include <__type_traits/is_assignable.h>
#include <__type_traits/is_constructible.h>
#include <__type_traits/is_convertible.h>
@ -152,7 +153,7 @@ struct _LIBCPP_TEMPLATE_VIS pair
template <class _Tuple>
using _CheckTLC _LIBCPP_NODEBUG = __conditional_t<
__tuple_like_with_size<_Tuple, 2>::value
&& !is_same<typename decay<_Tuple>::type, pair>::value,
&& !is_same<__decay_t<_Tuple>, pair>::value,
_CheckTupleLikeConstructor,
__check_tuple_constructor_fail
>;

View File

@ -111,9 +111,9 @@ private:
template <class _CharT, class _Traits, class _Delim>
_LIBCPP_HIDE_FROM_ABI ostream_joiner<typename decay<_Delim>::type, _CharT, _Traits>
_LIBCPP_HIDE_FROM_ABI ostream_joiner<__decay_t<_Delim>, _CharT, _Traits>
make_ostream_joiner(basic_ostream<_CharT, _Traits>& __os, _Delim && __d)
{ return ostream_joiner<typename decay<_Delim>::type, _CharT, _Traits>(__os, _VSTD::forward<_Delim>(__d)); }
{ return ostream_joiner<__decay_t<_Delim>, _CharT, _Traits>(__os, _VSTD::forward<_Delim>(__d)); }
_LIBCPP_END_NAMESPACE_LFTS

View File

@ -1760,7 +1760,7 @@ template <class _Fp>
__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
: __f_(nullptr)
{
typedef __libcpp_remove_reference_t<typename decay<_Fp>::type> _FR;
typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
if (sizeof(_FF) <= sizeof(__buf_))
{
@ -1784,7 +1784,7 @@ __packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
: __f_(nullptr)
{
typedef __libcpp_remove_reference_t<typename decay<_Fp>::type> _FR;
typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
if (sizeof(_FF) <= sizeof(__buf_))
{
@ -2193,10 +2193,10 @@ inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, lau
template <class _Fp, class... _Args>
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
async(launch __policy, _Fp&& __f, _Args&&... __args)
{
typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
typedef __async_func<__decay_t<_Fp>, __decay_t<_Args>...> _BF;
typedef typename _BF::_Rp _Rp;
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
@ -2219,7 +2219,7 @@ async(launch __policy, _Fp&& __f, _Args&&... __args)
template <class _Fp, class... _Args>
_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
async(_Fp&& __f, _Args&&... __args)
{
return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),

View File

@ -306,7 +306,7 @@ thread::thread(_Fp&& __f, _Args&&... __args)
{
typedef unique_ptr<__thread_struct> _TSPtr;
_TSPtr __tsp(new __thread_struct);
typedef tuple<_TSPtr, typename decay<_Fp>::type, typename decay<_Args>::type...> _Gp;
typedef tuple<_TSPtr, __decay_t<_Fp>, __decay_t<_Args>...> _Gp;
unique_ptr<_Gp> __p(
new _Gp(_VSTD::move(__tsp),
_VSTD::forward<_Fp>(__f),

View File

@ -434,7 +434,7 @@ template <class _Op, class _A0>
struct _UnaryOp
{
typedef typename _Op::__result_type __result_type;
typedef typename decay<__result_type>::type value_type;
using value_type = __decay_t<__result_type>;
_Op __op_;
_A0 __a0_;
@ -453,7 +453,7 @@ template <class _Op, class _A0, class _A1>
struct _BinaryOp
{
typedef typename _Op::__result_type __result_type;
typedef typename decay<__result_type>::type value_type;
using value_type = __decay_t<__result_type>;
_Op __op_;
_A0 __a0_;
@ -1117,7 +1117,7 @@ template <class _Op, class _Tp>
struct _UnaryOp<_Op, valarray<_Tp> >
{
typedef typename _Op::__result_type __result_type;
typedef typename decay<__result_type>::type value_type;
using value_type = __decay_t<__result_type>;
_Op __op_;
const valarray<_Tp>& __a0_;
@ -1136,7 +1136,7 @@ template <class _Op, class _Tp, class _A1>
struct _BinaryOp<_Op, valarray<_Tp>, _A1>
{
typedef typename _Op::__result_type __result_type;
typedef typename decay<__result_type>::type value_type;
using value_type = __decay_t<__result_type>;
_Op __op_;
const valarray<_Tp>& __a0_;
@ -1157,7 +1157,7 @@ template <class _Op, class _A0, class _Tp>
struct _BinaryOp<_Op, _A0, valarray<_Tp> >
{
typedef typename _Op::__result_type __result_type;
typedef typename decay<__result_type>::type value_type;
using value_type = __decay_t<__result_type>;
_Op __op_;
_A0 __a0_;
@ -1178,7 +1178,7 @@ template <class _Op, class _Tp>
struct _BinaryOp<_Op, valarray<_Tp>, valarray<_Tp> >
{
typedef typename _Op::__result_type __result_type;
typedef typename decay<__result_type>::type value_type;
using value_type = __decay_t<__result_type>;
_Op __op_;
const valarray<_Tp>& __a0_;