Home > Backend Development > C++ > How Can Extern Templates Optimize Template Instantiation in C ?

How Can Extern Templates Optimize Template Instantiation in C ?

Patricia Arquette
Release: 2024-12-12 21:52:14
Original
169 people have browsed it

How Can Extern Templates Optimize Template Instantiation in C  ?

Using Extern Template to Optimize Template Instantiation

In C 11, extern template is a powerful keyword that can be used to avoid redundant instantiation of templates, particularly when multiple translation units (e.g., .cpp files) may include the same header file containing template definitions.

Concept of Extern Template

Extern template essentially informs the compiler that the current translation unit should not instantiate a specific template, even though it is declared in the included header. This is useful when you know that the template will be instantiated in a different translation unit of the same project.

Usage for Function Templates

For function templates, an extern template declaration can be used as follows:

#include "header.h"
extern template void f<T>(); // Avoid instantiation in this translation unit
Copy after login

This indicates that the f template will be defined elsewhere in the project and should not be instantiated in the current file.

Usage for Class Templates

Similarly, for class templates, an extern template declaration takes the following form:

#include "header.h"
extern template class foo<int>; // Avoid instantiation in this translation unit
Copy after login

This ensures that the foo template class is not instantiated in this particular translation unit.

Optimization Applications

Consider the following scenario:

// header.h
template<typename T>
void f();
Copy after login
// source1.cpp
#include "header.h"
void f<int>();
Copy after login
Copy after login
// source2.cpp
#include "header.h"
void f<string>();
Copy after login

Without extern template, both source1.cpp and source2.cpp will instantiate the f template, leading to multiple definitions and wasted compilation time. By using extern template in one of the files, we avoid this redundancy:

// source1.cpp
#include "header.h"
void f<int>();
Copy after login
Copy after login
// source2.cpp
#include "header.h"
extern template void f<int>();
void f<string>();
Copy after login

Key Points

  • Use extern template only when you are certain that the template will be instantiated elsewhere in the project.
  • It applies to both function templates and class templates, as well as template member functions.
  • Using extern template can significantly reduce compilation time and optimize object file size, particularly when working with large or frequently used templates.

The above is the detailed content of How Can Extern Templates Optimize Template Instantiation in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template