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-67639eae3c0d4925387087/]

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

[crayon-67639eae3c0e1226327946/]

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

[crayon-67639eae3c0e3525531171/]

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-67639eae3c0e5643485355/]

Exit mobile version