在哪里/如何定义模板
在 C++ 中定义模板的最佳实践是什么?
template <class T>
class A
{
private:
// stuff
public:
T DoMagic()
{
//method body
}
}
Or:
template <class T>
class A
{
private:
// stuff
public:
T DoMagic();
}
template <class T>
A::T DoMagic()
{
// magic
}
另一种方式? 我似乎无意中发现了有关这个主题的一些争议。 所以;选择什么道路?
What is the best pratice in regards to defining a template in C++?
template <class T>
class A
{
private:
// stuff
public:
T DoMagic()
{
//method body
}
}
Or:
template <class T>
class A
{
private:
// stuff
public:
T DoMagic();
}
template <class T>
A::T DoMagic()
{
// magic
}
Another way?
I seem to stumble over some controversy regarding this subject.
So; What path to choose?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这完全是风格问题。不过,话虽如此:
没有真正的理由不包含所有内联定义(无论如何,它们都是从编译器 POV 内联的),但是,许多人认为将它们分开会更干净,并且允许类定义更具可读性。
This is completely a matter of style. That said however:
There's no real reason not to include all definitions inline (they are inline from the compilers POV anyway), however, many people argue that keeping them separate is more clean, and allows the class definition to be more readable.
随着您编写的模板变得越来越大、越来越复杂,请使用越来越高的分离级别。
无论如何将定义与声明分开,性能都是相同的,因此这里主要关注的应该是可读性和可维护性。
当编写仅在一个地方使用的简单模板时,请根据要使用它的 CPP 文件中的声明内联声明和定义它。如果只有一个代码块需要此模板,则没有理由强制进行全局重新编译。
file.cpp
对于跨翻译单元使用的小型模板实用程序,请在 H 文件中一起定义和声明它们:
utility.h
随着模板变得越来越复杂,能够从定义中单独查看声明将变得更加重要。首先,将所有内容分开,但放在同一个文件中:
utility.h
但最终,即使这样也会变得笨拙。当它出现时,将其分成一个用于声明的头文件和一个用于定义的 INC 文件。在头文件末尾,
#include
INC 文件:utility.h :
utility.inc :
Use an increasing level of separation as the templates you write grow larger and more complex.
Performance will be the same no matter how you separate the definitions from the declarations, so your main concern here should be readability and maintainability.
When writing a simple template used only in one place, declare and define it inline with the declarations right in the CPP file where you're going to use it. There's no reason to force a global recompile if only one block of code needs this template.
file.cpp
For small template utilities used across translation units, define and declare them together in an H file:
utility.h
As your templates become more complex it will become more important to be able to view the declaration separately from the definition. At first, keep everything separate but in the same file:
utility.h
But eventually even this will become unwieldly. When it does, separate it in to a header file for the declarations, and an INC file for the definitions. At the end of the header file,
#include
the INC file:utility.h :
utility.inc :
这是一个宗教(风格)问题。对于具有多个方法或少数方法很简单的类,我更喜欢在模板声明之外定义函数。
无论哪种情况,我的理解是模板声明和方法定义必须位于同一翻译单元中。这是因为
模板
更像是模板,编译器将给定类型插入模板并为给定类型生成代码。无论你做出什么决定,只要保持一致即可。
This is a religious (style) issue. I prefer to define my functions outside of the template declaration for classes that have more than one method or the few methods are simple.
In either case, my understanding is that the template declaration and the method definitions must be in the same translation unit. This is because the
template
is more like a stencil, the compiler plugs a given type into the stencil and generates code for the given type.Whatever you decide, just be consistent.
我通常在外部定义所有方法,但每次我希望 C++ 有某种“模板块”:
I usually define all the methods outside but each time I wish C++ had some sort of "template blocks":
如果函数很重要(即多于一行或两行),请考虑单独定义它们。这使得类的接口对于类的用户来说更容易导航、阅读和理解,他们很可能不必查看每个方法的实际实现。
If the functions are non-trivial (i.e. more than one or two lines), consider defining them separately. This makes the interface of the class much easier to navigate, read and understand for the users of your class, who most likely shouldn't have to look at the actual implementation of each method.
对于像您的示例这样的一次性实例,这几乎没有什么区别。
当存在大量具有多种变化的模板时会发生什么?然后,它有助于将相似类型的苹果放在一起,并将相似类型的橙子放在远离它们的地方。当然,这一切都必须直观且实用地完成。这很大程度上受到程序员使用代码的文化的影响。
For a one-off instance like your example, it makes little difference.
What happens when there are lots of templates with lots of variations? It then helps to put similar types of apples together, and similar types of oranges together away from them. Of course, this must all be done as intuitively as practical. That is greatly affected by the culture of programmers working with the code.