我有一些模板代码,我宁愿存储在一个CPP文件,而不是内联在头。我知道这是可以做到的,只要您知道将使用哪种模板类型。例如:

. h文件

class foo
{
public:
    template <typename T>
    void do(const T& t);
};

. cpp文件

template <typename T>
void foo::do(const T& t)
{
    // Do something with t
}

template void foo::do<int>(const int&);
template void foo::do<std::string>(const std::string&);

注意最后两行- foo::do模板函数只用于int和std::string,所以这些定义意味着应用程序将链接。

我的问题是-这是一个讨厌的黑客或这将与其他编译器/链接器工作吗?目前我只在VS2008中使用这段代码,但我想移植到其他环境中。


当前回答

This is definitely not a nasty hack, but be aware of the fact that you will have to do it (the explicit template specialization) for every class/type you want to use with the given template. In case of MANY types requesting template instantiation there can be A LOT of lines in your .cpp file. To remedy this problem you can have a TemplateClassInst.cpp in every project you use so that you have greater control what types will be instantiated. Obviously this solution will not be perfect (aka silver bullet) as you might end up breaking the ODR :).

其他回答

在最新的标准中,有一个关键字(export)可以帮助缓解这个问题,但在我所知道的任何编译器中都没有实现,除了Comeau。

请参阅FAQ-lite。

对于本页上的其他人来说,想知道显式模板专门化(至少在VS2008中)的正确语法是什么(和我一样),它如下…

在你的.h文件中…

template<typename T>
class foo
{
public:
    void bar(const T &t);
};

在你的。cpp文件中

template <class T>
void foo<T>::bar(const T &t)
{ }

// Explicit template instantiation
template class foo<int>;

这应该在支持模板的任何地方都能正常工作。显式模板实例化是c++标准的一部分。

是时候更新了!创建内联(。Inl,或可能是任何其他)文件,并简单地复制其中所有的定义。确保将模板添加到每个函数之上(template <typename T,…>)。现在,与在内联文件中包含头文件相反,您可以做相反的事情。在类声明之后包含内联文件(# Include "file.inl")。

我真的不知道为什么没有人提到这一点。我看不出有什么直接的缺点。

你举的例子没有问题。但是我必须说,我认为在cpp文件中存储函数定义是不有效的。我只理解需要将函数的声明和定义分开。

当与显式类实例化一起使用时,Boost Concept Check Library (BCCL)可以帮助您在cpp文件中生成模板函数代码。