Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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++?

like image 943
Adrian Avatar asked Feb 17 '26 02:02

Adrian


1 Answers

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).

like image 103
Angew is no longer proud of SO Avatar answered Feb 21 '26 14:02

Angew is no longer proud of SO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!