【发布时间】:2010-10-14 17:38:42
【问题描述】:
我正在尝试为类定义之外的显式专用类模板定义构造函数,如下所示:
template <typename T>
struct x;
template <>
struct x<int> {
inline x();
/* This would have compiled:
x() {
}
*/
};
template <> // Error
x<int>::x() {
}
但这似乎是一个错误。 Comeau 说:error: "x<int>::x()" is not an entity that can be explicitly specialized,尽管完整的课程是专门的。
这里有什么问题?
【问题讨论】:
-
我以前从未见过这个。在这里知道答案会很有趣。不过,一般来说,我会保持我的模板内联编码,以减少所有不必要的混乱。
-
使用 g++,我收到错误
template-id ‘x<>’ for ‘x<int>::x()’ does not match any template declaration。但是,如果我将其更改为不是构造函数,我仍然会收到等效错误:template-id ‘pants<>’ for ‘void x<int>::pants()’ does not match any template declaration. -
@Josh D:当然可以。事实上,对于没有有意义的泛型定义的
char_traits<>风格的类来说,这是唯一的选择 - 未定义特化的类参数将产生编译错误。 -
@Steve M. 我被误导了。谢谢你纠正我。冒犯性的谎言已被删除。
标签: c++ templates template-specialization