1

I am studying the C++ compilation process. By reading this SO post I have started to dig deeper. First, the preprocessor does its work and it resolves all of the preprocessor directives. On my g++ implementation (GNU), #import <iostream> is expanded to over 18k lines of source code.

However, the aforementioned post jumps to explain the process of syntactic validation of C++, i.e. the compiling stage, which translates the pure C++ code to the architecture-dependent assembly language.

However, before this translation step, I still see many templated definitions. Is there a step, prior to the translation to assembly, in where all of the templates are instantiated?

I know I can instruct g++ to only execute the preprocessing (-E option), but can I then instruct g++ to take the pure C++ file, without any preprocessing directives and generate a C++ file with all of the templates resolved?

Eduardo
  • 697
  • 8
  • 26

1 Answers1

2

Is there a step, prior to the translation to assembly, in where all of the templates are resolved?

No, there is no such separate step. Template code can and does depend on non-template code and vice versa, so they are processed together. Template code is also not specified to have any kind of equivalent non-template code in the C++ standard. Although one can come up with such template-to-non-template-code equivalence, modern compilers normally translate templates directly to some kind of internal representation rather than to non-template source code.

This is in contrast with preprocessor code, which can never depend on non-preprocessor code, and which always has precisely defined translation to non-preprocessor code. This translation is specified by the standard as one of the conceptual steps in the overall program translation process. Compilers usually implement this conceptual step as an actual translation step.

Eduardo
  • 697
  • 8
  • 26
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243