Adrian
Adrian

Reputation: 10911

Why doesn't member function signature have same specification as function signature?

Why does this work:

template <typename F>
struct getFnInfo;

template <typename RT, typename PT>
struct getFnInfo<RT(PT)>
{
    typedef RT R;
    typedef PT P;
};

But this doesn't:

template <typename CT, typename RT, typename PT>
struct getFnInfo<RT (CT::)(PT)>
{
    typedef CT C;
    typedef RT R;
    typedef PT P;
};

Do I just have the syntax wrong?

Note: this is used to extract type information like so:

int fn(double);
getFnInfo<decltype(fn)>::R // return type
getFnInfo<decltype(fn)>::P // parameter type

I know that there are other ways of doing this, but I'm curious what is stopping this from working? Is this just another annoying difference between functions and member functions in C++?

Upvotes: 3

Views: 65

Answers (1)

There is a type "function" in C++, but there is no type "member function" or "reference to member function." There is only "pointer to member function."

RT (CT::)(PT) is therefore syntactic nonsense. You'd need RT (CT::*)(PT), pointer to member function.

And since there is no "member function" type, there is even no legal way to refer to a member function in isolation. If you have a member function f inside a class C, then C::f by itself is an error again. You need to call it (something.f() or C::f()) or take its address (&C::f).

Upvotes: 5

Related Questions