Skeen
Skeen

Reputation: 4722

Checking attributes of a function pointer?

Assume that I've got the following piece of code, in a generic context;

auto function = T::getFunctionPtr();

Is it possible to check whether 'function' is a global function versus a static class method, at compile time?

Upvotes: 1

Views: 143

Answers (1)

bolov
bolov

Reputation: 75727

The only way to check at compile time if 'function' is a global function or a static class method is to check the return type of 'T::getFunctionPtr()'. But global functions and static class methods have the same type, as shown here:

#include <iostream>
#include <typeinfo>
using namespace std;

void GlobalFunc() {}

class A {
  public:
    static void StaticFunc() {}
};

int main() {
  cout << std::boolalpha;
  cout << (typeid(GlobalFunc) == typeid(A::StaticFunc)) << endl;
  return 0;
}

outputs "true". http://ideone.com/oo70F9 . So the answer is no, you can't do this at compile time.

Upvotes: 2

Related Questions