Site icon Learn C++

Stages of C++ Templates Compilation On Windows

In order to compile C++ templates the compilers like Embarcadero RAD Studio C++ compilers are required to perform two stages: definition stage and instantiation stage.

The template definition stage

On this stage the following checks are performed:

The complete check of unqualified names is postponed until the template instantiation stage when the template arguments are known.

Please note, since the compilers like Embarcadero RAD Studio C++ performs many checks on this stage a programmer is often see general problems even before the template instantiation stage!

Example of errors even when the branch is discarded at compile-time:

[crayon-6620dee38520b979252920/]

Example of syntax error upon checking of unqualified names which are depends on template parameters:

[crayon-6620dee385212842043622/]

Another example of syntax error upon checking the construct before period that depends on a template parameter:

[crayon-6620dee385213149028359/]

The template instantiation stage

The first point where the template is used for a first time for particular template arguments is called the point of instantiation. At this point the template arguments are known hence the compilers are able to generate specializations of templates for concrete arguments. During the generation of a specialization:

Please note, that functions and classes generated from templates are subject to the same rules as for regular functions and classes.

Example of errors during the template instantiation stage:

[crayon-6620dee385215552253411/]

Exit mobile version