Why can templates only be implemented in the header file?
来源:互联网 发布:validate.js 示例 编辑:程序博客网 时间:2024/06/05 02:21
Quote from The C++ standard library: a tutorial and handbook:
The only portable way of using templates at the moment is to implement them in header files by using inline functions.
Why is this?
Because when instantiating a template, the compiler creates a new class with the given template argument. For example:
template<typename T>struct Foo{ T bar; void doSomething(T param) {/* do stuff using T */}};// somewhere in a .cppFoo<int> f;
When reading this line, the compiler will create a new class (let's call it FooInt
), which is equivalent to the following:
struct FooInt{ int bar; void doSomething(int param) {/* do stuff using int */}}
Consequently, the compiler needs to have access to the implementation of the methods, to instantiate them with the template argument (in this case int
). If these implementations were not in the header, they wouldn't be accessible, and therefore the compiler wouldn't be able to instantiate the template.
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.
// Foo.htemplate <typename T>struct Foo{ void doSomething(T param);};#include "Foo.tpp"// Foo.tpptemplate <typename T>void Foo<T>::doSomething(T param){ //implementation}
This way, implementation is still separated from declaration, but is accessible to the compiler.
Another solution is to keep the implementation separated, and explicitly instantiate all the template instances you'll need:
// Foo.h// no implementationtemplate <typename T> struct Foo { ... };//---------------------------------------- // Foo.cpp// implementation of Foo's methods// explicit instantiationstemplate class Foo<int>;template class Foo<float>;// You will only be able to use Foo with int or float
If my explanation isn't clear enough, you can have a look at the C++ FaqLite on this subject.
- Why can templates only be implemented in the header file?
- Why only SIP INVITE can be CANCELed ?
- An error occurred at line: 7 in the generated java file Only a type can be imported. java.util.list
- why 'not in' can not be followed with 'null'
- Only one expression can be specified in the select list when the subquery is not introduced with(求助)
- Only one expression can be specified in the select list when the subquery is not introduced with EXI
- An explicit value for the identity column in table 'users' can only be specified when a column list
- Array constants can only be used in initializers
- exchange only can be changed in process method
- Pods written in Swift can only be integrated as frameworks
- Pods written in Swift can only be integrated as frameworks
- Pods written in Swift can only be integrated as frameworks
- Error: CompareBaseObjectsInternal can only be called from the main thread
- unity DestroyBuffer can only be called from the main thread
- get_isPlaying can only be called from the main thread unity3d
- Warning C4819:The file contains a character that can not be represented in the current code page(936
- Warning C4819:The file contains a character that can ot be represented in the current code page(936)
- objc[2213]: Class _NSZombie_BSXPCMessage is implemented in both ?? and ??. One of the two will be us
- 用友HCM,一位杰出的建筑师:用企业数据搭建信息大厦
- Android 4.1 Netd详细分析(三)代码分析1
- GNU Emacs Manual 24.3 第三章
- Getting Started with PHPUnit
- Matlab读取文件方法
- Why can templates only be implemented in the header file?
- 黑马程序员-day13-String类
- C++习题 对象数组求最大值
- Android 如何判断一个应用在运行
- 个人观点,说一下对 PHPCMS 的网站架构的看法
- POJ 1474 Video Surveillance(半平面交)
- HDU 4576 Robot(概率dp)
- php/mysql/jquery实现各系统流行的瀑布流显示方式
- 芭萨提的颜色 豆瓣影评 我辈只能奋不顾身 挽救于万一