Movatterモバイル変換


[0]ホーム

URL:


Next:, Previous:, Up:Extensions to the C++ Language   [Contents][Index]


8.6 Extracting the Function Pointer from a Bound Pointer to Member Function

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

Note that you still pay the penalty for the call through afunction pointer; on most modern architectures, such a call defeats thebranch prediction features of the CPU. This is also true of normalvirtual 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 beconverted to function pointers directly:

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

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


[8]ページ先頭

©2009-2026 Movatter.jp