Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ How is this not member function pointer?

Tags:

c++

typetraits

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?

like image 785
Alexander Bily Avatar asked Apr 01 '16 06:04

Alexander Bily


People also ask

Why the pointer this Cannot be used in static member functions?

First, because static member functions are not attached to an object, they have no this pointer! This makes sense when you think about it -- the this pointer always points to the object that the member function is working on. Static member functions do not work on an object, so the this pointer is not needed.

Do all member functions have a this pointer?

The this pointer is a pointer accessible only within the nonstatic member functions of a class , struct , or union type. It points to the object for which the member function is called. Static member functions don't have a this pointer.

How do you create a pointer to a member function?

Using a pointer-to-member-function to call a function Calling the member function on an object using a pointer-to-member-function result = (object. *pointer_name)(arguments); or calling with a pointer to the object result = (object_ptr->*pointer_name)(arguments);

Which is not a member function?

Non-member Function: The function which is declared outside the class is known as the non-member function of that class. Below is the difference between the two: The member function can appear outside of the class body (for instance, in the implementation file).


2 Answers

int A::*() is type of function, which returns A's member with type int, takes no arguments. So it's not member function pointer, even not function pointer.

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 parentheses change the precedence, int (A::*)() is type of A's member function pointer, which returns int and takes no arguments.

like image 62
songyuanyao Avatar answered Sep 22 '22 19:09

songyuanyao


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…

like image 28
Cheers and hth. - Alf Avatar answered Sep 19 '22 19:09

Cheers and hth. - Alf