Chapter 7. Extensions to the C++ Language
If you use one big file to do the instantiations, you may want to compile it without
-fno-implicit-templates
instantiations (but not by any other files) without having to specify them as well.
g++ has extended the template instantiation syntax given in the ISO standard to allow forward
declaration of explicit instantiations (with
for a template class (i.e. the vtable) without instantiating any of its members (with
and instantiation of only the static data members of a template class, without the support data
or member functions (with (
extern template int max (int, int);
inline template class Foo int ;
static template class Foo int ;
3. Do nothing. Pretend g++ does implement automatic instantiation management. Code written
for the Borland model will work fine, but each translation unit will contain instances of each
of the templates it uses. In a large program, this can lead to an unacceptable amount of code
duplication.
Section 7.5 Declarations and Definitions in One Header, for more discussion of these pragmas.
7.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
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
the address of the function. They can be converted to function pointers directly:
fptr p1 = (fptr)(&A::foo);
You must specify
-Wno-pmf-conversions
7.8. C++-Specific Variable, Function, and Type Attributes
Some attributes only make sense for C++ programs.
so you get all of the instances required by your explicit
extern
):
static
&Klasse::Member
to use this extension.
), instantiation of the compiler support data
), no object is needed to obtain
217
),
inline
this
Need help?
Do you have a question about the ENTERPRISE LINUX 3 - USING GCC and is the answer not in the manual?
Questions and answers