C/C++ and Fortran

C/C++ and Fortran

Connect, learn, share, and engage with IBM Power.

 View Only

The XL C/C++ V11.1 compiler supports extern templates

By Archive User posted Wed March 20, 2013 04:14 AM

  

Originally posted by: sumi_cj


Extern templates, also known as explicit instantiation declarations, are introduced as a new feature in the C++11 standard. This feature aims to improve compiler performance. Let's see how template instantiation works without extern templates from the following example:

// a.h
template <typename T> int f(T){}

// 1.cpp
#include a.h
int fun1(){
    f(1);
}

// 2.cpp
#include a.h
int fun2(){
   f(2);
}

In this example,  function template f(T) is declared in a.h. When the compiler compiles 1.cpp, template f(T) is implicitly instantiated as f<int>(int).  The same thing happens during the compilation of 2.cpp. Both 1.o and 2.o files have the instantiation definition of the f<int>(int) function.  If the compiler needs to link these two object files later, it will keep only one copy of the instantiation definition and remove the other one.
Can we find possible problems from this example?  We can imagine that if function template f(T) is instantiated many times in the program, the compiler has to repeat the instantiation procedure at every appearance of the function template and remove the duplicated instantiation definitions at the link phase. The compiler generates a lot of redundant code and the compile and link time is unnecessarily wasted.
Feature extern template are proposed in the C++11 standard to resolve this issue. In fact, extern templates follow the same principle as extern declarations of variables that we are familiar with. See the following example:
//1.c
int i= 1;

//2.c
#include <stdio.h>
extern int i;
printf(“The value of i is %d”, i);

In this example,  object files 1.o and 2.o only have one copy of variable i. Variable i is defined in 1.c and reused in 2.c  through the extern declaration. We can extend this idea to templates. Let's revise the first example as follows:

// a.h
template <typename T> int f(T){}
extern template int f<int>(int);

// 1.cpp
#include a.h
template int f<int>(int);
int fun1(){
    f(1);
}

// 2.cpp
#include a.h
int fun2(){
   f(2);
}

In the revised example, function template f(T) is explicitly instantiated in file 1.cpp, and its  explicit instantiation declaration is in a.h. In file 2.cpp, the compiler assumes that f(T)is already instantiated in another compile unit through the extern template and will not instantiate the template again. By using the extern template, the implicit instantiation of a template is suppressed, which helps reduce the collective size of the object files and shorten compile and link time.
We need to keep in mind the following restrictions of this new feature when using it.
1. Extern templates apply to both function templates and class templates.
2. If an explicit instantiation declaration of a function template exists, but there is no corresponding explicit instantiation definition in the program, the linker will issue an error message.
3. You can not declare an explicit instantiation for a static function template because a static function cannot be accessed by names in other compile units.
4. The C++11 standard does not specify whether an explicit instantiation declaration works for an inline function. Therefore, the behavior of the compiler is undefined.
5. Do not overuse extern templates. You only need to use this feature for the templates that are implicitly instantiated frequently.

Authors: Sumi Chen (Jing Chen), Steven Zhang (Qing Shan Zhang)
0 comments
0 views

Permalink