(C++) 我把所有模板放在哪里?
我有不同的类,全部按层次结构排列。 为了减少代码量,我开始创建模板函数。基类将使用模板函数,一些派生类也将使用相同的函数。我应该把所有这些模板放在哪里,这样我就不会像以前一样遇到未定义的参考问题?我是否应该将所有定义放在一个头文件中,然后将该头文件包含在调用函数的类的 .cpp 部分中。那行得通吗?截至目前,我的所有类(class.cpp、class.h)都可以正常编译,但在链接过程中一切都会崩溃。我尝试将所有模板放入命名空间中,然后将该命名空间包含在所有类的实现中,但这似乎不起作用。我的问题是,您将如何创建一个单独的实体,该实体仅包含任何类都可以在其数据成员上使用的模板化函数?
I have different classes all arranged in a hierarchy.
To reduce the amount of code, I started creating template functions. The base class will use the template functions and some of the derived classes will also use the same functions. Where am I suppose to put all of these templates so I don't get undefined reference issues like I have been? Should I put all the definitions in a header file and then just include that header files in the .cpp part of the class that call the functions. will that work? As of right now, all of my classes(class.cpp, class.h) compile fine, but everything blows up during the linking. I tried to put all the templates in a namespace and then include that namespace in the implementation of all my classes but that doesn't seem to work. My question is, how would you go about making a separate entity that just holds templated functions that any class can use on it's data members?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
模板函数和模板类的定义属于头文件,而不是 .cpp 文件。
这是因为编译器本质上必须为
#include
作为标头的文件中使用的每组模板参数生成一个全新的函数。如果模板函数是在 .cpp 文件中定义的,那么必须在不知道调用代码是什么样子的情况下生成这些函数的所有适当版本,这基本上是不可能的。 (通过这种方式,您确实获得了模板函数的重复定义,但链接器会删除这些定义,并确保最终二进制文件中的每个模板实例化都只有一份副本。)The definitions of template functions and template classes belong in header files, not .cpp files.
This is because the compiler essentially has to generate a brand new function for each set of template parameters that's used in the file that
#include
s the header. If the template function were defined in a .cpp file, then all of the appropriate versions of these functions would have to be generated without knowing what the calling code looks like, and that's basically impossible. (You do get duplicate definitions of template functions this way, but the linker removes those and makes sure there's only one copy if each template instantiation in the final binary.)我看到很多人对这个东西感到困惑......模板不是类型。
它们在实例化时变成类型。
因此,模板的成员必须位于您要使用它们的同一数据单元中。
如果您的模板是通用的并且您想在所有代码中使用它,只需将所有内容放入头文件中即可。
现在,如果您不喜欢(我会理解)在同一个文件中显示声明、定义和实现,您可以将模板拆分在两个不同的文件中。
例如,“list.h”包含您的声明,“list.inc”包含您的实现。
为了使其发挥作用,您必须将两者都包含在内。
I see a lot of people confused by this thing... templates are not types.
They become types when instantiated.
For this reason members of templates must stay in the same data unit you are going to use them.
If you template is generic and you want to use it in all your code, just put everything in header files.
Now, if you don't like (and i would understand that) that you show declarations and definitions and implementation all in the same file, you can split templates in two different files.
For example, "list.h" with your declaration and "list.inc" with your implementation.
To make it work, you have to include both.