Skip to content

Commit 7415a7f

Browse files
[libc++][NFC] Simplify SFINAE detections in weak_result_type.h (#169870)
For `__has_result_type`, it can be replaced with a variable template `__has_result_type_v`. Note that the pre-existing extraneous `*` used in detection is buggy, but it's a functional change to fix it. `false_type` and `true_type` are no longer directly used, so direct inclusion of `<__type_traits/integral_constant.h>` is removed. For `__derives_from_{unary,binary}_function`, it's unnecessary to invent a `__two` type for each specialization. So `void` is used instead. Also, `nullptr` is now used instead of `0`.
1 parent 1ab64e4 commit 7415a7f

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

libcxx/include/__functional/weak_result_type.h

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
#include <__config>
1414
#include <__functional/binary_function.h>
1515
#include <__functional/unary_function.h>
16-
#include <__type_traits/integral_constant.h>
1716
#include <__type_traits/invoke.h>
1817
#include <__type_traits/is_same.h>
18+
#include <__type_traits/void_t.h>
1919
#include <__utility/declval.h>
2020

2121
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -24,50 +24,36 @@
2424

2525
_LIBCPP_BEGIN_NAMESPACE_STD
2626

27-
template <class _Tp>
28-
struct __has_result_type {
29-
private:
30-
template <class _Up>
31-
static false_type __test(...);
32-
template <class _Up>
33-
static true_type __test(typename _Up::result_type* = 0);
27+
template <class _Tp, class = void>
28+
inline const bool __has_result_type_v = false;
3429

35-
public:
36-
static const bool value = decltype(__test<_Tp>(0))::value;
37-
};
30+
template <class _Tp>
31+
inline const bool __has_result_type_v<_Tp, __void_t<typename _Tp::result_type*> > = true;
3832

3933
// __weak_result_type
4034

4135
template <class _Tp>
4236
struct __derives_from_unary_function {
4337
private:
44-
struct __two {
45-
char __lx;
46-
char __lxx;
47-
};
48-
static __two __test(...);
38+
static void __find_base(...);
4939
template <class _Ap, class _Rp>
50-
static __unary_function<_Ap, _Rp> __test(const volatile __unary_function<_Ap, _Rp>*);
40+
static __unary_function<_Ap, _Rp> __find_base(const volatile __unary_function<_Ap, _Rp>*);
5141

5242
public:
53-
static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
54-
typedef decltype(__test((_Tp*)0)) type;
43+
using type = decltype(__find_base(static_cast<_Tp*>(nullptr)));
44+
static const bool value = !is_same<type, void>::value;
5545
};
5646

5747
template <class _Tp>
5848
struct __derives_from_binary_function {
5949
private:
60-
struct __two {
61-
char __lx;
62-
char __lxx;
63-
};
64-
static __two __test(...);
50+
static void __find_base(...);
6551
template <class _A1, class _A2, class _Rp>
66-
static __binary_function<_A1, _A2, _Rp> __test(const volatile __binary_function<_A1, _A2, _Rp>*);
52+
static __binary_function<_A1, _A2, _Rp> __find_base(const volatile __binary_function<_A1, _A2, _Rp>*);
6753

6854
public:
69-
static const bool value = !is_same<decltype(__test((_Tp*)0)), __two>::value;
70-
typedef decltype(__test((_Tp*)0)) type;
55+
using type = decltype(__find_base(static_cast<_Tp*>(nullptr)));
56+
static const bool value = !is_same<type, void>::value;
7157
};
7258

7359
template <class _Tp, bool = __derives_from_unary_function<_Tp>::value>
@@ -85,7 +71,7 @@ struct __maybe_derive_from_binary_function // bool is true
8571
template <class _Tp>
8672
struct __maybe_derive_from_binary_function<_Tp, false> {};
8773

88-
template <class _Tp, bool = __has_result_type<_Tp>::value>
74+
template <class _Tp, bool = __has_result_type_v<_Tp> >
8975
struct __weak_result_type_imp // bool is true
9076
: public __maybe_derive_from_unary_function<_Tp>,
9177
public __maybe_derive_from_binary_function<_Tp> {

0 commit comments

Comments
 (0)