std :: is_function的实现 - 为什么我的实现行为不同?

Ale*_*ily 5 c++ templates type-traits

我有以下is_function的实现:

template <typename SomeType>
struct _is_function_helper : public _false_expression {};
template <typename ReturnType, typename ... ArgumentTypes>
struct _is_function_helper<ReturnType (ArgumentTypes ...)> : _true_expression {};
template <typename ReturnType, typename ... ArgumentTypes>
struct _is_function_helper<ReturnType (ArgumentTypes ..., ...)> : _true_expression {};

template <typename SomeType>
struct _is_function : public _boolean_expression<_is_function_helper<typename _remove_cv<typename _remove_reference<SomeType>::Type>::Type>::value> {};
Run Code Online (Sandbox Code Playgroud)

我删除引用,cv限定符,然后尝试继承与_is_function_helper相同的bool表达式.然后我尝试了以下测试:

void func(int,int) { };

struct A { void foo(int); };

....

auto r = func;
std::cout << std::boolalpha;
std::cout << std::is_function<decltype(func)>::value << " " << _is_function<decltype(func)>::value << std::endl;
std::cout << std::is_function<int(int)>::value << " " << _is_function<int(int)>::value << std::endl;
std::cout << std::is_function<int(*)(int)>::value << " " << _is_function<int(*)(int)>::value << std::endl;
std::cout << std::is_function<decltype(r)>::value << " " << _is_function<decltype(r)>::value << std::endl;
std::cout << std::is_function<decltype(*r)>::value << " " << _is_function<decltype(*r)>::value << std::endl;
std::cout << std::is_function<decltype(&A::foo)>::value << " " << _is_function<decltype(&A::foo)>::value << std::endl;
Run Code Online (Sandbox Code Playgroud)

以下是这些测试的输出:

true true
true true
false false
false false
false true
false false
Run Code Online (Sandbox Code Playgroud)

我有两个问题:

  1. 为什么第5个测试用例的输出不同?
  2. 如何使用_is_function检测struct的成员函数?

Tar*_*ama 5

第五种情况下的输出是不同的,因为decltype(*r)它是对函数的引用.您的实现删除了此引用,但std::is_function没有.

您可以通过添加如下的特化来检测成员函数指针:

template <typename ReturnType, typename ... ArgumentTypes, typename T>
struct _is_function_helper<ReturnType (T::*) (ArgumentTypes ...)> 
    : _true_expression {};
Run Code Online (Sandbox Code Playgroud)