C++11: External templates (Extern template)

Source: Internet
Author: User

In the c++98/03 language standard, the compiler needs to instantiate every template that appears in the source code, and the linker will need to remove the duplicated instantiation code when linking. Obviously, it is obviously unnecessary to have the compiler go through the repetitive instantiation process every time, and the connector is therefore affected.
In the real world of coding, a software implementation might use the same type to instantiate the same template in many blocks of code. At this point, if you can let the compiler avoid such repetitive instantiation work, you can greatly provide compiler productivity. Therefore, there is an urgent need for a means (a declarative way) to tell the compiler that the template instantiation work that occurred there has been done in other compilation units and no longer needs to be instantiated.
As a result, a new language feature ———— external templates (Extern template) ———— is incorporated into the C++0X standard.
In c++98/03, there is already a language feature called Display instantiation (Explicit instantiation), whose purpose is to instruct the compiler to instantiate the template immediately (that is, to force instantiation). The external template syntax is modified based on the syntax of the display instantiation instruction: The syntax of the external template is obtained by adding the prefix extern before the instantiation instruction is displayed.
Display instantiation Syntax: template class vector<myclass>;
External templates Syntax: extern template class vector<myclass>;
Once an external template declaration (such as extern template class vector<myclass>;) is used in a compilation unit, the compiler skips the template instantiation that matches the external template declaration (such as vector<) when compiling the compilation unit. myclass>).
Therefore, in c++0x, "display instantiation directives for templates, external template directives, and use" can be likened to the definition, declaration, and use of global variables. The only difference is that the template represents the code, and the variable represents a contiguous memory space.
Some constraints on the external template syntax:
1. If an external template directive appears in a compilation unit, the corresponding display instantiation must appear in another compilation unit or in subsequent code in the same compilation unit.
2. External template directives cannot be used with a static function (that is, a file-domain function), but can be used for class-static functions. (Note: This is obvious because static functions do not have external connection properties and are not likely to appear outside this compilation unit).
3. External template directives are applied to the members of the class, not to the class itself. (Note: Since it has been used for all members of the class, it is equivalent to a class)
4. External templates are useful for inline functions and are not explicitly specified, but are encouraged by the compiler implementation.
Compiler support
Visual Studio seems to support external templates starting with VS2008 (VC9). GCC seems to have supported external templates very early, but it is unclear which version to add, at least 4.3 and later versions are supported.
Ref
1. Adding "extern template" (version 2)
2. C++0x,extern Template @ Wiki

C++11: External templates (Extern template)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.