完全专业化模板中构造函数的外联定义 [英] Out-of-line definition of constructor in fully-specialized template
问题描述
我真的很惭愧提出这个问题,但看起来我毕竟对模板一无所知.
I am really ashamed to ask this question, but it looks like I don't know anything about templates after all.
所以我有这个片段:
template <typename> class foo;
class bar;
template <> class foo <bar>
{
public:
foo();
};
template <> foo <bar> :: foo()
{
}
哪里,好吧,我只有一个模板类 foo
、一个类 bar
、一个带有构造函数的特殊化 foo
,我想定义该构造函数.
Where, well, I just have a template class foo
, a class bar
, a specialization foo <bar>
with a constructor, and I would like to define that constructor out of line.
尽管这个例子看起来很琐碎,但我无法编译它,而且我总是得到 没有函数模板匹配函数模板特化 'foo'
.
As trivial as this example might look like, I can't get it to compile, and I always get No function template matches function template specialization 'foo'
.
如果我添加了一个虚拟参数,使得模板不是完全专门化的(例如 template <bool dummy> foo <bar, dummy> :: foo()
),它运行良好.我错过了什么?
If I add a dummy parameter so that the template is not fully specialized (e.g. template <bool dummy> foo <bar, dummy> :: foo()
) it works nicely. What am I missing?
推荐答案
可以使用普通成员定义语法定义完整类模板特化的成员.这不是模板的定义,因此不能指定 template<>
前缀.
The members of a full class template specialization can be defined using the ordinary member definition syntax. This is not definition for template, so template<>
prefix cannot be specified.
只需将其更改为
foo <bar> :: foo()
{
}
这篇关于完全专业化模板中构造函数的外联定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!