I want to write a trait-checker named is_pure_func_ptr, which can determine if the type is a pure function pointer, as follows:
#include <iostream>
using namespace std;
void f1()
{};
int f2(int)
{};
int f3(int, int)
{};
struct Functor
{
void operator ()()
{}
};
int main()
{
cout << is_pure_func_ptr<decltype(f1)>::value << endl; // output true
cout << is_pure_func_ptr<decltype(f2)>::value << endl; // output true
cout << is_pure_func_ptr<decltype(f3)>::value << endl; // output true
cout << is_pure_func_ptr<Functor>::value << endl; // output false
cout << is_pure_func_ptr<char*>::value << endl; // output false
}
My question is: How to implement it?
As stated by Joachim Pileborg,
std::is_functionwill do the job.If that isn’t an option for you, but you do have C++11 support (meaning you just want to know how to implement it yourself or your standard library isn’t there yet), you could do something like this:
This works, but you might need additional work when it comes to supporting functions with different calling conventions and/or cv-qualified pointers