Skip to content

Commit c9cde35

Browse files
tniessenaduh95
authored andcommitted
src: simplify is_callable by making it a concept
Using a C++20 `concept` here makes `is_callable` much simpler than relying on SFINAE. It is equivalent for function types, `std::function`, lambdas, and classes with `operator()`, regardless of argument or return types. PR-URL: #58169 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent 892b425 commit c9cde35

File tree

2 files changed

+3
-7
lines changed

2 files changed

+3
-7
lines changed

src/req_wrap-inl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ struct CallLibuvFunction<ReqT, void(*)(ReqT*, Args...)> {
106106
template <typename ReqT, typename T>
107107
struct MakeLibuvRequestCallback {
108108
static T For(ReqWrap<ReqT>* req_wrap, T v) {
109-
static_assert(!is_callable<T>::value,
109+
static_assert(!is_callable<T>,
110110
"MakeLibuvRequestCallback missed a callback");
111111
return v;
112112
}

src/util.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -660,13 +660,9 @@ struct MallocedBuffer {
660660
};
661661

662662
// Test whether some value can be called with ().
663-
template <typename T, typename = void>
664-
struct is_callable : std::is_function<T> { };
665-
666663
template <typename T>
667-
struct is_callable<T, typename std::enable_if<
668-
std::is_same<decltype(void(&T::operator())), void>::value
669-
>::type> : std::true_type { };
664+
concept is_callable =
665+
std::is_function<T>::value || requires { &T::operator(); };
670666

671667
template <typename T, void (*function)(T*)>
672668
struct FunctionDeleter {

0 commit comments

Comments
 (0)