Skip to content

Commit 4e0876e

Browse files
authored
[libc++] Use __underlying_type directly in underyling_type_t (#135423)
This avoids instantiating multiple classes, reducing compile times. This patch also introduces `__underyling_type_t` for internal use, similar to other type traits.
1 parent 1004fae commit 4e0876e

File tree

6 files changed

+18
-9
lines changed

6 files changed

+18
-9
lines changed

libcxx/include/__atomic/memory_order.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2424
// to pin the underlying type in C++20.
2525
enum __legacy_memory_order { __mo_relaxed, __mo_consume, __mo_acquire, __mo_release, __mo_acq_rel, __mo_seq_cst };
2626

27-
using __memory_order_underlying_t _LIBCPP_NODEBUG = underlying_type<__legacy_memory_order>::type;
27+
using __memory_order_underlying_t _LIBCPP_NODEBUG = __underlying_type_t<__legacy_memory_order>;
2828

2929
#if _LIBCPP_STD_VER >= 20
3030

@@ -37,7 +37,7 @@ enum class memory_order : __memory_order_underlying_t {
3737
seq_cst = __mo_seq_cst
3838
};
3939

40-
static_assert(is_same<underlying_type<memory_order>::type, __memory_order_underlying_t>::value,
40+
static_assert(is_same<__underlying_type_t<memory_order>, __memory_order_underlying_t>::value,
4141
"unexpected underlying type for std::memory_order");
4242

4343
inline constexpr auto memory_order_relaxed = memory_order::relaxed;

libcxx/include/__functional/hash.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ struct hash<long double> : public __scalar_hash<long double> {
504504
template <class _Tp, bool = is_enum<_Tp>::value>
505505
struct __enum_hash : public __unary_function<_Tp, size_t> {
506506
_LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
507-
typedef typename underlying_type<_Tp>::type type;
507+
using type = __underlying_type_t<_Tp>;
508508
return hash<type>()(static_cast<type>(__v));
509509
}
510510
};

libcxx/include/__type_traits/underlying_type.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
_LIBCPP_BEGIN_NAMESPACE_STD
2020

21-
template <class _Tp, bool = is_enum<_Tp>::value>
21+
template <class _Tp, bool>
2222
struct __underlying_type_impl;
2323

2424
template <class _Tp>
@@ -32,9 +32,18 @@ struct __underlying_type_impl<_Tp, true> {
3232
template <class _Tp>
3333
struct _LIBCPP_NO_SPECIALIZATIONS underlying_type : __underlying_type_impl<_Tp, is_enum<_Tp>::value> {};
3434

35+
// GCC doesn't SFINAE away when using __underlying_type directly
36+
#if !defined(_LIBCPP_COMPILER_GCC)
37+
template <class _Tp>
38+
using __underlying_type_t _LIBCPP_NODEBUG = __underlying_type(_Tp);
39+
#else
40+
template <class _Tp>
41+
using __underlying_type_t _LIBCPP_NODEBUG = typename underlying_type<_Tp>::type;
42+
#endif
43+
3544
#if _LIBCPP_STD_VER >= 14
3645
template <class _Tp>
37-
using underlying_type_t = typename underlying_type<_Tp>::type;
46+
using underlying_type_t = __underlying_type_t<_Tp>;
3847
#endif
3948

4049
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__utility/convert_to_integral.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __uint128_t __convert_to_integral
5050

5151
template <class _Tp, bool = is_enum<_Tp>::value>
5252
struct __sfinae_underlying_type {
53-
typedef typename underlying_type<_Tp>::type type;
53+
using type = __underlying_type_t<_Tp>;
5454
typedef decltype(((type)1) + 0) __promoted_type;
5555
};
5656

libcxx/include/__utility/to_underlying.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2121

2222
#ifndef _LIBCPP_CXX03_LANG
2323
template <class _Tp>
24-
_LIBCPP_HIDE_FROM_ABI constexpr typename underlying_type<_Tp>::type __to_underlying(_Tp __val) noexcept {
25-
return static_cast<typename underlying_type<_Tp>::type>(__val);
24+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI constexpr __underlying_type_t<_Tp> __to_underlying(_Tp __val) noexcept {
25+
return static_cast<__underlying_type_t<_Tp>>(__val);
2626
}
2727
#endif // !_LIBCPP_CXX03_LANG
2828

libcxx/include/future

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
440440

441441
# ifndef _LIBCPP_CXX03_LANG
442442

443-
typedef underlying_type<launch>::type __launch_underlying_type;
443+
using __launch_underlying_type _LIBCPP_NODEBUG = __underlying_type_t<launch>;
444444

445445
inline _LIBCPP_HIDE_FROM_ABI constexpr launch operator&(launch __x, launch __y) {
446446
return static_cast<launch>(static_cast<__launch_underlying_type>(__x) & static_cast<__launch_underlying_type>(__y));

0 commit comments

Comments
 (0)