c++ - 这被认为是显式 C++ 模板类实例化吗?

标签 c++ templates header-files

<分区>

Possible Duplicate:
Why can templates only be implemented in the header file?
Why should the implementation and the declaration of a template class be in the same header file?

我是某所大学的计算机科学专业的学生,​​我们得到了用于硬件的文件。 而且我不确定这种实例化是如何工作的。

长代码短它看起来像这样。

在List.h中

#ifndef _LIST_H_
#define _LIST_H_

#include <iterator>
#include <ostream>

template <class T>
class List

/* implementation below but not relevant to this post */
.
.
.
.

....下面文件的最后几行。

#include "list.cpp"

#include "list_given.cpp"

#endif

并且List.cpp没有包含List.h

我不明白在头文件中包含 List.cpp 是如何工作的。

最佳答案

#include 只是导致文本替换,没有别的,所以就好像 list.cpp 的全部内容在头文件中被复制了一样。这与“显式模板实例化”无关。

list.cpp 不包含 header ,否则 header 会递归地包含自身。

关于c++ - 这被认为是显式 C++ 模板类实例化吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9456484/

相关文章:

c++ - 可变参数模板 模板

asp.net-mvc - 使用 ASP.NET MVC 作为电子邮件模板引擎 : Good Idea?

c++ - 为什么在头文件中声明并在文件中定义会出现多重定义错误?

C 头文件 <stdio.h>

C 头文件和动态链接错误

c++ - 为什么 CopyFile2 会替换 CopyFile 和 CopyFileEx?

c++ - 使用 C++ 和 glm 转换位置数组

c++ - 如何在boost序列化中使用数组优化

c++ - 当使用从 operator new 返回的指针作为指向数组元素的指针时,这是未定义的行为吗

javascript - Synergy 模板不会运行 &lt;script&gt; 代码