C ++ How is this not a member function pointer?

According to the following test:

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

It is not a pointer to a member function, but a regular function, and this:

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

is true. I tried both with gcc and msvc. What is the difference between these two ads? Are these results correct? Why are parentheses around A::* important?

+5
source share
2 answers

Differences in expressions with different expressions in parentheses due to operator precedence.

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

  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::*() is of type int A::* (*)() .

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

Now we go for coffee & hellip;

+3
source

int A::*() - a function type that returns a member A type int , does not accept arguments. Therefore, it is not a pointer to a member function, not even a pointer to a function.

 std::cout << std::is_member_function_pointer<int A::*()>::value << std::endl; // 0 std::cout << std::is_pointer<int A::*()>::value << std::endl; // 0 std::cout << std::is_function<int A::*()>::value << std::endl << std::endl; // 1 

And the brackets change the priority, int (A::*)() is the type of the function pointer A , which returns int and does not accept arguments.

+5
source

Source: https://habr.com/ru/post/1246205/


All Articles