我有以下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> {};
我删除了引用,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;
这是这些测试的输出:
true true
true true
false false
false false
false true
false false
我有两个问题:
第5种情况下的输出是不同的,因为decltype(*r)
它是对函数的引用。您的实现将删除此引用,但std::is_function
不会删除。
您可以通过添加以下特殊化来检测成员函数指针:
template <typename ReturnType, typename ... ArgumentTypes, typename T>
struct _is_function_helper<ReturnType (T::*) (ArgumentTypes ...)>
: _true_expression {};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句