I am preparing a short presentation on templates for work and am using isocpp.org as a starting point for the content. However, I have come across an interesting paragraph:
A note to the experts: I have obviously made several simplifications above. This was intentional so please don’t complain too loudly. If you know the difference between a .cpp file and a compilation unit, the difference between a class template and a template class, and the fact that templates really aren’t just glorified macros, then don’t complain: this particular question/answer wasn’t aimed at you to begin with. I simplified things so newbies would “get it,” even if doing so offends some experts.
There are a few things that confuse me, one of which is .cpp file vs compilation unit.
I have read that a single .cpp file is called a "translation unit". But what exactly is a compilation unit?
I found this answer that says translation and compilation units are the same thing, and basically just fancy names for a single .cpp file, but isocpp seems to think otherwise, and google only gives explanations for translation units even if I search "compilation unit". Wikipedia seems to mention the "Single Compilation Unit" model, but gives no insight on what actually is a compilation unit.
So what is a compilation unit and how is it different from a translation unit (and is a translation unit 100% the same thing as a .cpp file)?
CodePudding user response:
First, translation and compilation units are the same thing. The word/phrase Translation unit is used more often than compilation unit. Which basically means your source file including all of its header files.
Second we(and by we i mean good C books) use the term function template
or class template
rather than using the terms "template function" and "template class".
From documentation
The text of the program is kept in units called source files in this International Standard. A source file together with all the headers (17.6.1.2) and source files included (16.2) via the preprocessing directive #include, less any source lines skipped by any of the conditional inclusion (16.1) preprocessing directives, is called a translation unit
Also note in the same document they have used the term compilation unit. And if you read carefully the use of the word compilation unit in that document, you will see that they mean the same thing as translation unit.
Now to clear everything up(from above),
- a compilation and a translation unit are the same thing.
- a cpp file alone(without its headers) does not constitute a translation unit(or compilation unit since they mean the same thing). On the other hand a cpp file with all of its headers included does constitute a translation/compilation unit.
CodePudding user response:
A translation unit is the result of including all headers in the CPP file - not CPP file alone.