这是有效的 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
, but 海湾合作委员会-4.5.1抱怨:
prog.cpp:3:5:错误:无效使用不完整类型“class any_iterator”
prog.cpp:2:11:错误:“class any_iterator”的声明
prog.cpp:在函数“int main()”中:
prog.cpp:21:11:错误:“class any_iterator”没有名为“foo”的成员
prog.cpp:在构造函数“any_iterator::any_iterator() [withcategory = int]”中:
prog.cpp:20:27:从这里实例化
prog.cpp:7:44:错误:类型“any_iterator”不是“any_iterator”的直接基础
有人可以引用显示是否应该编译的标准吗?我认为这是 MSVC 中的一个错误。
作为注释,我知道正确的做法是声明类,专门化根,then定义一般情况,这就是我将对我的代码执行的操作,但我想知道哪个编译器在这里是错误的?
要从某个类型继承,该类型必须是完整的。稍微重新安排一下就可以解决问题:
template<class category>
class any_iterator;
template<>
class any_iterator<void>
{
public:
typedef any_iterator<void> any_iter_void;
any_iterator() { }
void foo() { }
};
template<class category>
class any_iterator : public any_iterator<void>
{
public:
typedef any_iterator<void> any_iter_void;
any_iterator() : any_iter_void() { }
};
int main()
{
any_iterator<int> a;
a.foo();
}
代币标准报价:
C++11,第 10/2 节:
由 a 表示的类型基类型说明符应该是一个不是不完全定义的类的类类型;这个类称为直接基类对于正在定义的类。
§9.2/2:
类在结束时被认为是完全定义的对象类型(或完整类型)}
of the 类说明符.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)