为什么模板只能在头文件中实现? [英] Why can templates only be implemented in the header file?

查看:243
本文介绍了为什么模板只能在头文件中实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

报价http://books.google.com/books?id=n9VEG2Gp5pkC&pg=PA10&lpg=PA10&dq=%22The%20only%20portable%20way%20of%20using%20templates %20原子%20the%20moment%20是%20to%20implement%20them%20英寸%20header%20files%20by%20using%20inline%20functions%22&安培;源= BL&安培; OTS = Ref8pl8dPX&安培; SIG = t4K5gvxtBblpcujNxodpwMfei8I&安培; HL = EN与放; EI = qkR6TvbiGojE0AHq4IzqAg&安培; SA = X&安培; OI = book_result&安培; CT =导致&安培; resnum = 3及VED = 0CC8Q6AEwAg#v = onepage&安培; q =%二条%20only%20portable%20way%20of%20using%20templates%20AT%20the%20moment%20是%20to%20implement%20them%20英寸%20header%20files%20by%20using%20inline%20functions%22&安培; F = FALSE> C ++标准库:教程和手册:

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.

这是为什么

(澄清:头文件不是的只有的便携式解决方案但他们。是最方便的便携式解决方案。)

(Clarification: header files are not the only portable solution. But they are the most convenient portable solution.)

推荐答案

这是的的必要把实施中头文件,看到这个答案的末尾替代解决方案。

It is not necessary to put the implementation in the header file, see the alternative solution at the end of this answer.

总之,你的代码是失败的原因是,实例化一个模板时,编译器创建一个新的类与给定的模板参数。例如:

Anyway, the reason your code is failing is that, 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 .cpp
Foo<int> f; 



< > FooInt ),相当于以下内容:

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 */}
}


$ b b

因此,编译器需要访问方法的实现,使用template参数(在这种情况下 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.

这是一个常见的解决方案是在头文件中写模板声明,然后在实现文件(例如.tpp)中实现该类,并在头的末尾包含这个实现文件。

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.h
template <typename T>
struct Foo
{
    void doSomething(T param);
};

#include "Foo.tpp"

// Foo.tpp
template <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 implementation
template <typename T> struct Foo { ... };

//----------------------------------------    
// Foo.cpp

// implementation of Foo's methods

// explicit instantiations
template class Foo<int>;
template class Foo<float>;
// You will only be able to use Foo with int or float

不够清楚,您可以查看此主题上的C ++超级常见问题 a>。

If my explanation isn't clear enough, you can have a look at the C++ Super-FAQ on this subject.

这篇关于为什么模板只能在头文件中实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆