Implementation of std :: is_function - why does my implementation behave differently?

I have an implementation of 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> {}; 

I delete links, cv qualifiers, and then try to inherit from the same bool expression as _is_function_helper. Then I tried the following tests:

 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; 

And here is the output of these tests:

 true true true true false false false false false true false false 

I have two questions:

  • Why is the conclusion different in the fifth test case?
  • How can a member function of a structure be detected using _is_function?
+5
source share
1 answer

The output in the fifth case is different from decltype(*r) being a function reference. Your implementation removes this link, but std::is_function does not work.

You can detect a pointer to a member function by adding specialization as follows:

 template <typename ReturnType, typename ... ArgumentTypes, typename T> struct _is_function_helper<ReturnType (T::*) (ArgumentTypes ...)> : _true_expression {}; 
+5
source

Source: https://habr.com/ru/post/1245697/


All Articles