Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is_function_pointer<> for <type_traits>

There are these in <type_traits>:

is_pointer<>
is_function<>
is_member_function_pointer<>

But not this:

is_function_pointer<>

Why is it so?

like image 601
Nubcase Avatar asked Jul 03 '11 01:07

Nubcase


1 Answers

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
like image 106
Howard Hinnant Avatar answered Oct 04 '22 14:10

Howard Hinnant