Web page hosting, cheap domain name & website promotion services
  

 Index

6.7 Extracting the function pointer from a bound pointer to member function

In C++, pointer to member functions (PMFs) are implemented using a wide pointer of sorts to handle all the possible call mechanisms; the PMF needs to store information about how to adjust the this pointer, and if the function pointed to is virtual, where to find the vtable, and where in the vtable to look for the member function. If you are using PMFs in an inner loop, you should really reconsider that decision. If that is not an option, you can extract the pointer to the function that would be called for a given object/PMF pair and call it directly inside the inner loop, to save a bit of time.

Note that you will still be paying the penalty for the call through a function pointer; on most modern architectures, such a call defeats the branch prediction features of the CPU. This is also true of normal virtual function calls.

The syntax for this extension is

     extern A a;
     extern int (A::*fp)();
     typedef int (*fptr)(A *);
     
     fptr p = (fptr)(a.*fp);

For PMF constants (i.e. expressions of the form &Klasse::Member), no object is needed to obtain the address of the function. They can be converted to function pointers directly:

     fptr p1 = (fptr)(&A::foo);

You must specify -Wno-pmf-conversions to use this extension.

 

 

  

 

Domain registration & domain transfer | Register cheap domain name 

Disclaimers: This document is provided as free service for the benefits of Active-Venture.com's virtual hosting customers only.

 

 

" It reminds me of the claim that Americans built the first computer... It depends on what properties are necessary for a device to be classed as a computer: That it's electronic? That it has Randomly Accessible Memory? That it operates on a stored program? I am tempted to suggest that one of the requirements implicit in some people's lists is that it was built in America.   "