Home > Backend Development > C++ > How Can `extern template` Prevent Redundant Template Instantiation in C ?

How Can `extern template` Prevent Redundant Template Instantiation in C ?

Barbara Streisand
Release: 2024-12-20 20:42:09
Original
495 people have browsed it

How Can `extern template` Prevent Redundant Template Instantiation in C  ?

Using extern template to Avoid Template Instantiation

Template instantiation is a crucial step in C programming, where specific template instances are created at compile time. In certain scenarios, it becomes necessary to control template instantiation to optimize compilation time and avoid redundant definitions. This is where the extern template keyword comes into play.

Extern Templates for Function Templates

When used with function templates, extern template instructs the compiler not to instantiate the specified template within the current translation unit. It signals that an instantiation of that template will be found elsewhere in the program. This is particularly useful in scenarios where multiple source files instantiate the same template with identical parameters, leading to multiple definitions that the compiler must discard.

Example:

Consider the following code snippet:

In this case, the extern template in source2.cpp informs the compiler that the f template instantiation should be taken from elsewhere. This prevents the compiler from generating a redundant instantiation and discarding it later during linking.

Extern Templates for Class Templates

Similar to function templates, extern template can also be used with class templates. This is done to avoid multiple instantiations of template classes with the same parameters.

Example:

Here, the extern template in source2.cpp ensures that the MyClass template class is instantiated only once, preventing redundant definitions.

Guidelines for Using extern template

  • Use extern template only when you are certain that an instantiation of the template exists elsewhere in the program.
  • This technique can be applied to function templates, class templates, and even template member functions.
  • It is generally advisable to keep all template definitions and instantiations within a single header file to avoid potential conflicts.
  • If it is not possible to centralize template definitions, use extern template in all but one source file where the template is used.

The above is the detailed content of How Can `extern template` Prevent Redundant 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