Why use a “tpp” file when implementing templated functions and classes defined in a header?

后端 未结 3 1718
礼貌的吻别
礼貌的吻别 2020-12-05 07:27

Please refer to the first answer in this question about implementing templates.

Specifically, take note of this quote

A common solution to thi

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 08:16

    Does it matter if it's .tpp or any other extension? And why not use a .cpp?

    It doesn't matter much which extension is actually used, as long it is different from any of the standard extensions used for C++ translation units.

    The reasoning is to have a different file extension as they are usually detected by any C++ build systems for translation units (.cpp, .cc, ...). Because translating these as a source file would fail. They have to be #included by the corresponding header file containing the template declarations.

    But if the implementation file has some funky looking file extension, how does that work in terms of compiling?

    It needs to be #included to be compiled as mentioned.

    Is it as efficient as if the implementations were in a cpp?

    Well, not a 100% as efficient regarding compile time like a pure object file generated from a translation unit. It will be compiled again, as soon the header containing the #include statement changes.

    And are the implementations compiled every time I compile whatever source code #includes said header?

    Yes, they are.

提交回复
热议问题