is_function_pointer<> for <type_traits>
There are these in <type_traits>
:
is_pointer<&g开发者_高级运维t;
is_function<>
is_member_function_pointer<>
But not this:
is_function_pointer<>
Why is it so?
The traits in [meta.unary.cat] are intended to classify each type into a single category. Is it a void, integral, pointer, etc. At this level, pointer-to-function is no different than pointer-to-int. And note that a pointer to a member is not a pointer. It is merely an english homonym.
It was intended that every type return true to exactly one trait in [meta.unary.cat]. And in this categorization, both a function pointer and a scalar pointer would both return true under is_pointer
.
I will note that we did not achieve our objective. nullptr_t
escapes our goal. But we got close. Here is a graphical representation of the current type_traits classification.
Update:
This is a correctly working program with correct output:
#include <iostream>
#include <type_traits>
typedef void (*fptr)();
typedef int* intptr;
int main()
{
std::cout << std::is_function<fptr>::value << '\n';
std::cout << std::is_pointer<fptr>::value << '\n';
std::cout << std::is_pointer<intptr>::value << '\n';
}
0
1
1
It does seem like an odd oversight. However, member pointers are always member pointer types, unlike free functions, which can be either pointers types (void(*)()
) or function types (void()
). Also std::is_pointer
never returns true for member pointer types.
However, if you need this functionality, here's an implementation:
template<typename testType>
struct is_function_pointer
{
static const bool value =
std::is_pointer<testType>::value ?
std::is_function<typename std::remove_pointer<testType>::type>::value :
false;
};
精彩评论