gcc: Bound member functions
1
1 7.6 Extracting the Function Pointer from a Bound Pointer to Member Function
1 ===========================================================================
1
1 In C++, pointer to member functions (PMFs) are implemented using a wide
1 pointer of sorts to handle all the possible call mechanisms; the PMF
1 needs to store information about how to adjust the 'this' pointer, and
1 if the function pointed to is virtual, where to find the vtable, and
1 where in the vtable to look for the member function. If you are using
1 PMFs in an inner loop, you should really reconsider that decision. If
1 that is not an option, you can extract the pointer to the function that
1 would be called for a given object/PMF pair and call it directly inside
1 the inner loop, to save a bit of time.
1
1 Note that you still pay the penalty for the call through a function
1 pointer; on most modern architectures, such a call defeats the branch
1 prediction features of the CPU. This is also true of normal virtual
1 function calls.
1
1 The syntax for this extension is
1
1 extern A a;
1 extern int (A::*fp)();
1 typedef int (*fptr)(A *);
1
1 fptr p = (fptr)(a.*fp);
1
1 For PMF constants (i.e. expressions of the form '&Klasse::Member'), no
1 object is needed to obtain the address of the function. They can be
1 converted to function pointers directly:
1
1 fptr p1 = (fptr)(&A::foo);
1
1 You must specify '-Wno-pmf-conversions' to use this extension.
1