c ++模板和头文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5612791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:31:20  来源:igfitidea点击:

c++ template and header files

c++templatesfileheader

提问by kamikaze_pilot

So, I heard that C++ templates shouldn't be separated into a header (.h) and source (.cpp) files.

所以,我听说 C++ 模板不应该被分成标头 (.h) 和源 (.cpp) 文件。

For instance, a template like this:

例如,这样的模板:

template <class T>
class J
{   
   T something;
};

Is this true? Why is it so?

这是真的?为什么会这样?

If because of that I'm gonna have to put both declaration and implementation in the same file, should I put it in a .h file or a .cpp file?

如果因此我必须将声明和实现放在同一个文件中,我应该将它放在 .h 文件中还是 .cpp 文件中?

采纳答案by Lightness Races in Orbit

Headers.

标题。

It's because templates are instantiated at compile-time, not link-time, and different translation units (roughly equivalent to your .cppfiles) only "know about" each other at link-time. Headers tend to be widely "known about" at compile-time because you #includethem in any translation unit that needs them.

这是因为模板是在编译时而不是链接时实例化的,并且不同的翻译单元(大致相当于您的.cpp文件)仅在链接时“了解”彼此。头文件往往在编译时被广泛“了解”,因为您#include在任何需要它们的翻译单元中都使用它们。

Read https://isocpp.org/wiki/faq/templatesfor more.

阅读https://isocpp.org/wiki/faq/templates了解更多信息。

回答by Chris A.

The reason you can't put a templated class into a .cpp file is because in order to "compile" a .cpp file you need to know what the type that is being used in place of T. As it stands a templated class (like your class J) doesn't have enough information to compile. Thus it must be all in headers.

不能将模板类放入 .cpp 文件的原因是为了“编译”一个 .cpp 文件,您需要知道代替 T 使用的类型。因为它代表模板类(就像你的班级 J) 没有足够的信息来编译。因此它必须全部在标题中。

If you want the break up the implementation into another file for cleanliness, the best practice is to use an .hxx file. Like this: inside your header file, J.h, put:

如果您希望将实现分解为另一个文件以保持整洁,最佳做法是使用 .hxx 文件。像这样:在你的头文件 Jh 中,输入:

#ifndef _J_H__
#define _J_H__

template <class T> class J{  // member definitions };

#include "j.hxx"

#endif // _J_H__

and then, in j.hxx you'll have

然后,在 j.hxx 中,您将拥有

template <class T> J<T>::J() { // constructor implementation }

template <class T> J<T>::~J() { // destructor implementation }

template <class T> void J<T>::memberFunc() { // memberFunc implementation }

// etc.

Finally in your .cpp file that uses the templated class, let's call it K.cpp you'll have:

最后,在使用模板化类的 .cpp 文件中,我们将其称为 K.cpp,您将拥有:

#include "J.h" // note that this always automatically includes J.hxx    
void f(void)
{
     J<double> jinstance;  // now the compiler knows what the exact type is.
}

回答by jonsca

Yes, it's true. Declaration and implementation are generally put into the header file all together. Some compilers experimented with an exportkeyword that would allow them to be separated, but that has been removed from C++0x. Check out this FAQ entryfor all the dirty details.

对,是真的。声明和实现一般都放在头文件中。一些编译器试验了一个export允许它们分开的关键字,但它已从 C++0x 中删除。查看此常见问题条目以了解所有肮脏的细节。

回答by John Zwinck

If you need the template code to be usable by other translation units (.cpp files), you need to put the implementation in the .h file or else those other units won't be able to instantiate the template (expand it according to the types they use).

如果您需要模板代码可供其他翻译单元(.cpp 文件)使用,则需要将实现放在 .h 文件中,否则其他翻译单元将无法实例化模板(根据他们使用的类型)。

If your template function is only instantiated in one .cpp file, you can define it there. This happens sometimes when a class has a private member function which is a template (and it is only called from the implementation file, not the class header file).

如果您的模板函数仅在一个 .cpp 文件中实例化,您可以在那里定义它。当一个类有一个私有成员函数是一个模板时,有时会发生这种情况(它只从实现文件而不是类头文件中调用)。