【发布时间】:2012-01-15 09:18:17
【问题描述】:
这是有效的 C++ 吗?
template<class category>
class any_iterator : public any_iterator<void>
{
public:
typedef any_iterator<void> any_iter_void;
any_iterator() : any_iter_void() {}
};
template<>
class any_iterator<void>
{
public:
typedef any_iterator<void> any_iter_void;
any_iterator() {}
void foo() {};
};
int main() {
any_iterator<int> a;
a.foo();
}
MSVC10 在\WALL 上接受它并且没有错误/警告,但gcc-4.5.1 抱怨:
prog.cpp:3:5: 错误:无效使用不完整类型 'class any_iterator'
prog.cpp:2:11:错误:“类 any_iterator”的声明
prog.cpp:在函数“int main()”中:
prog.cpp:21:11:错误:“类 any_iterator”没有名为“foo”的成员
prog.cpp: 在构造函数中'any_iterator::any_iterator() [with category = int]':
prog.cpp:20:27: 从这里实例化
prog.cpp:7:44:错误:类型“any_iterator”不是“any_iterator”的直接基础
有人可以引用标准来说明这是否应该编译吗?我认为这是 MSVC 中的一个错误。
作为说明,我知道正确的做法是声明类,专门化根,然后定义一般情况,这就是我将对我的代码执行的操作,但我想知道这里哪个编译器出错了?
【问题讨论】:
-
MSVC 缺少两阶段名称查找的问题再次出现。
-
与其对基类使用特化,为什么不完全使用不同的类?
-
MSVC 的实现似乎更方便 IMO。
-
@MarkRansom:(真正的)类形成一个层次结构,用户可以在其中添加类型(它们是迭代器类别)。因此,
any_iterator<void>作为一切的根源是有意义的。
标签: c++ templates inheritance template-specialization