C ++ How is it not the member function pointer?

advertisements

According to following test this:

std::cout << std::is_member_function_pointer<int A::*()>::value << std::endl;

Is not member function pointer, but regular function, while this:

std::cout << std::is_member_function_pointer<int (A::*)()>::value << std::endl;

evaluates to true. I tried both with gcc & msvc. What is difference between these two declarations? Are these results correct? Why are parenthesis around A::* important?


The differences of differently parenthesized type expressions, stem from the operator precedence.

Here is one way to get a more detailed, descriptive specification of a type:

C:\my\forums\so\120> echo struct A{}; using T = int A::*(); T o; int x = o; >1.cpp

C:\my\forums\so\120> g++ -c 1.cpp
1.cpp:1:48: error: invalid conversion from 'int A::* (*)()' to 'int' [-fpermissive]
 struct A{}; using T = int A::*(); T o; int x = o;
                                                ^

C:\my\forums\so\120> _

So, we see that a variable of type int A::*() has the type int A::* (*)().

EDIT: I cannot delete this post while it's marked as solution, so for the record: in the above code o is not a variable. Instead it's a function declaration. int A::*() is directly a function type, namely a function returning a data member pointer.

Now heading for coffee…