Reputation: 2554
In the following code, I have a function member and a data member which is a function pointer. For use in a declaration or as a template parameter, the type of a pointer to the function member f
is int (Test::*)(int)
; but what is the analogous type of a pointer to the function pointer data member pf
?
struct Test
{
int f( int ) { return 0; };
int (*pf)( int );
};
int main()
{
using Fmem = int (Test::*)( int );
Fmem x = &Test::f;
// using PFmem = ??;
// PFmem y = &Test::pf;
}
Upvotes: 0
Views: 144
Reputation: 38538
struct Test
{
int f( int ) { return 0; };
int (*pf)( int );
};
int main()
{
using Fmem = int (Test::*)( int );
Fmem x = &Test::f;
using PFmem = int (* Test::*)( int );
PFmem y = &Test::pf;
}
If you have doubts you could deduce the type with help of the compiler error message:
using PFmem = int; // any type
PFmem y = &Test::pf;
The code above causes the error message below:
prog.cpp:13:30: error: cannot convert ‘int (* Test::*)(int)’ to ‘int’ in assignment
PFmem y = &Test::pf;
From this error message we now know the required type is int (* Test::*)(int)
.
Upvotes: 2
Reputation: 85767
using PFmem = int (*Test::*)( int );
// ^ a pointer ...
// ^^^^^^ to a member of Test ...
// ^ which is a pointer ...
// ^ ^ to a function ...
// ^^^ taking an int ...
// ^^^ returning an int
You can also try to untangle the syntax with template typedefs (I'm not sure if it's worth it):
struct Test
{
int f( int ) { return 0; };
int (*pf)( int );
};
template<typename T> using pointer_to = T *;
template<class C, typename T> using pointer_to_member_of = T C::*;
template<typename R, typename ...A> using function = R (A ...);
int main()
{
using Fmem = int (Test::*)( int );
Fmem x = &Test::f;
using PFmem = int (*Test::*)( int );
PFmem y = &Test::pf;
pointer_to_member_of<Test, function<int, int>>
x1 = &Test::f;
pointer_to_member_of<Test, pointer_to<function<int, int>>>
y2 = &Test::pf;
}
Upvotes: 3