]>
Commit | Line | Data |
---|---|---|
1 | .. | |
2 | Copyright 1988-2022 Free Software Foundation, Inc. | |
3 | This is part of the GCC manual. | |
4 | For copying conditions, see the copyright.rst file. | |
5 | ||
6 | .. index:: pmf, pointer to member function, bound pointer to member function | |
7 | ||
8 | .. _bound-member-functions: | |
9 | ||
10 | Extracting the Function Pointer from a Bound Pointer to Member Function | |
11 | *********************************************************************** | |
12 | ||
13 | In C++, pointer to member functions (PMFs) are implemented using a wide | |
14 | pointer of sorts to handle all the possible call mechanisms; the PMF | |
15 | needs to store information about how to adjust the :samp:`this` pointer, | |
16 | and if the function pointed to is virtual, where to find the vtable, and | |
17 | where in the vtable to look for the member function. If you are using | |
18 | PMFs in an inner loop, you should really reconsider that decision. If | |
19 | that is not an option, you can extract the pointer to the function that | |
20 | would be called for a given object/PMF pair and call it directly inside | |
21 | the inner loop, to save a bit of time. | |
22 | ||
23 | Note that you still pay the penalty for the call through a | |
24 | function pointer; on most modern architectures, such a call defeats the | |
25 | branch prediction features of the CPU. This is also true of normal | |
26 | virtual function calls. | |
27 | ||
28 | The syntax for this extension is | |
29 | ||
30 | .. code-block:: c++ | |
31 | ||
32 | extern A a; | |
33 | extern int (A::*fp)(); | |
34 | typedef int (*fptr)(A *); | |
35 | ||
36 | fptr p = (fptr)(a.*fp); | |
37 | ||
38 | For PMF constants (i.e. expressions of the form :samp:`&Klasse::Member`), | |
39 | no object is needed to obtain the address of the function. They can be | |
40 | converted to function pointers directly: | |
41 | ||
42 | .. code-block:: c++ | |
43 | ||
44 | fptr p1 = (fptr)(&A::foo); | |
45 | ||
46 | .. index:: Wno-pmf-conversions | |
47 | ||
48 | You must specify :option:`-Wno-pmf-conversions` to use this extension. |