What is a “translation unit” in C++

前端 未结 11 688
既然无缘
既然无缘 2020-11-21 07:56

I am reading at the time the \"Effective C++\" written by Meyers and came across the term \"translation unit\".

Could somebody please give me an explanation of:

11条回答
  •  野性不改
    2020-11-21 08:29

    A hard question to answer definitively. The C++ standard states:

    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.4.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. [Note: a C++ program need not all be translated at the same time. ]

    So for most intents and purposes a translation unit is a single C++ source file and the header or other files it includes via the preprocessor #include mechanism.

    Regarding your other questions:

    2) When should I consider using it when programming with C++

    You can't not consider it - translation units are the basis of a C++ program.

    3) If it is related only to C++, or it can be used with other programming languages

    Other languages have similar concepts, but their semantics will be subtly different. Most other languages don't use a preprocessor, for example.

提交回复
热议问题