为什么要使用“ tpp”实现标头中定义的模板化函数和类时的文件? [英] Why use a "tpp" file when implementing templated functions and classes defined in a header?

查看:137
本文介绍了为什么要使用“ tpp”实现标头中定义的模板化函数和类时的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅此问题中的第一个答案关于实现模板的信息。

具体来说,请注意该报价

Specifically, take note of this quote


对此的一种常见解决方案是将模板声明写入头文件中,然后在实现文件中实现该类(例如.tpp ),并在该实现文件的末尾包含此实现文件。

A common solution to this is to write the template declaration in a header file, then implement the class in an implementation file (for example .tpp), and include this implementation file at the end of the header.

我加粗了我最感兴趣的部分。

a的意义是什么。 tpp文件?我尝试完全按照该页面中的建议进行操作,并且有效。但是后来,我将文件扩展名更改为任意乱码(例如.zz或.ypp),并且仍然有效!它应该工作吗? .tpp或任何其他扩展名有关系吗?为什么不使用.cpp?

I bolded the part that I'm most interested in.
What's the significance of a .tpp file? I tried doing exactly what was suggested in that page and it worked. But then, I changed the file extension to any random gibberish (like .zz or .ypp) and it still worked! Is it supposed to work? Does it matter if it's .tpp or any other extension? And why not use a .cpp?

这是我感到困惑的另一件事。

如果我的实现是用.cpp编写的,并且标头定义了非模板函数,那么我只需要编译一次.cpp文件,对吗?至少直到我更改了.cpp文件中的内容。

Here's another thing I'm confused about.
If my implementations were written in a .cpp and the header defined non-templated functions, then I'd only need to compile the .cpp file once, right? at least until I changed something in the .cpp file.

但是,如果我有一个定义模板函数的标头,并且我的实现位于一个带有随机时髦扩展名的文件中,那么它将如何编译?每次我编译源代码 #include 所说的标头时,实现是否每次都会编译?

But if I have a header defining templated functions and my implementations are in a file with a random funky extension, how is it compiled? And are the implementations compiled every time I compile whatever source code #includes said header?

推荐答案


.tpp或任何其他扩展名是否重要?为什么不使用.cpp?

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

扩展名无关紧要,但不要使用 .cpp ,因为它违反了约定(它仍然可以工作,但是不要这样做; .cpp 文件通常是源文件)。除此之外,还取决于您的代码库使用什么。例如,我(和Boost代码库)为此使用 .ipp

It does not matter what the extension is, but don't use .cpp because it goes against conventions (it will still work, but don't do it; .cpp files are generally source files). Other than that it's a matter of what your codebase uses. For example I (and the Boost codebase) use .ipp for this purpose.


.tpp文件的意义是什么?

What's the significance of a .tpp file?

当您不希望包含模块接口的文件包含所有详细的实现细节时使用。但是您不能在 .cpp 文件中编写实现,因为它是模板。因此,您将尽力而为(不考虑显式实例化等)。例如

It's used when you don't want the file that contains the interface of a module to contain all the gory implementation details. But you cannot write the implementation in a .cpp file because it's a template. So you do the best you can (not considering explicit instantiations and the like). For example

Something.hpp

#pragma once

namespace space {

template <typename Type>
class Something {
public:
    void some_interface();
};

} // namespace space

#include "Something.ipp"

Something.ipp

#pragma once

namespace space {

template <typename Type>
void Something<Type>::some_interface() {
    // the implementation
}

} // namespace space








我认为在标头和单独文件中的实现是为了节省编译时间,因此您只编译一次实现,直到进行一些更改为止。

I thought the whole point of writing definitions in headers and the implementations in a separate file is to save compilation time, so that you compile the implementations only once until you make some changes

t将通用模板代码拆分为一个实现文件。您需要完整的可见代码才能使用模板,这就是为什么您需要将所有内容都放在头文件中的原因。有关更多信息,请参见为什么只能在标头中实现模板文件?

You can't split up general template code into an implementation file. You need the full code visible in order to use the template, that's why you need to put everything in the header file. For more see Why can templates only be implemented in the header file?


但是,如果实现文件具有一些看起来很时髦的文件扩展名,那么在编译方面该如何工作?

But if the implementation file has some funky looking file extension, how does that work in terms of compiling? Is it as efficient as if the implementations were in a cpp?

您不编译 .tpp吗? .ipp -inl.h 等文件。它们就像头文件一样,只不过它们仅包含在其他头文件中。您只能编译源文件( .cpp .cc )文件。

You don't compile the .tpp, .ipp, -inl.h, etc files. They are just like header files, except that they are only included by other header files. You only compile source (.cpp, .cc) files.

这篇关于为什么要使用“ tpp”实现标头中定义的模板化函数和类时的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆