]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/doc/gcc/extensions-to-the-c++-language/extracting-the-function-pointer-from-a-bound-pointer-to-member-function.rst
sphinx: add missing trailing newline
[thirdparty/gcc.git] / gcc / doc / gcc / extensions-to-the-c++-language / extracting-the-function-pointer-from-a-bound-pointer-to-member-function.rst
CommitLineData
c63539ff
ML
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
10Extracting the Function Pointer from a Bound Pointer to Member Function
11***********************************************************************
12
13In C++, pointer to member functions (PMFs) are implemented using a wide
14pointer of sorts to handle all the possible call mechanisms; the PMF
15needs to store information about how to adjust the :samp:`this` pointer,
16and if the function pointed to is virtual, where to find the vtable, and
17where in the vtable to look for the member function. If you are using
18PMFs in an inner loop, you should really reconsider that decision. If
19that is not an option, you can extract the pointer to the function that
20would be called for a given object/PMF pair and call it directly inside
21the inner loop, to save a bit of time.
22
23Note that you still pay the penalty for the call through a
24function pointer; on most modern architectures, such a call defeats the
25branch prediction features of the CPU. This is also true of normal
26virtual function calls.
27
28The 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
38For PMF constants (i.e. expressions of the form :samp:`&Klasse::Member`),
39no object is needed to obtain the address of the function. They can be
40converted to function pointers directly:
41
42.. code-block:: c++
43
44 fptr p1 = (fptr)(&A::foo);
45
46.. index:: Wno-pmf-conversions
47
3ed1b4ce 48You must specify :option:`-Wno-pmf-conversions` to use this extension.