继承自专业化的类?
这是有效的 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: 错误: 声明“class any_iterator”
prog.cpp:在函数“int main()”中:
prog.cpp:21:11:错误:“class 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 中的一个错误。
作为注释,我知道正确的做法是声明类,专门化根,然后定义一般情况,这就是我将对代码执行的操作,但我想知道哪个这里编译器出错了?
Is this valid 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 accepts it with no errors/warnings on \WALL
, but gcc-4.5.1 complains:
prog.cpp:3:5: error: invalid use of incomplete type 'class any_iterator'
prog.cpp:2:11: error: declaration of 'class any_iterator'
prog.cpp: In function 'int main()':
prog.cpp:21:11: error: 'class any_iterator' has no member named 'foo'
prog.cpp: In constructor 'any_iterator::any_iterator() [with category = int]':
prog.cpp:20:27: instantiated from here
prog.cpp:7:44: error: type 'any_iterator' is not a direct base of 'any_iterator'
Can someone quote the standard showing if this should or should not compile? I think this is a bug in MSVC.
As a note, I know the correct thing to do is to declare the class, specialize the root, then define the general case, and that's what I'll do to my code, but I was wondering which compiler is wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要从某个类型继承,该类型必须是完整的。稍微重新安排一下就可以解决问题:
令牌标准引用:
C++11,§10/2:
§9.2/2:
To inherit from a type, that type must be complete. A little rearranging solves things:
Token standard quotes:
C++11, §10/2:
§9.2/2:
10/2:
这是 MSVC 缺陷的一种表现:它缺乏两阶段名称解析。
10/2:
It is one manifestation of the bug of MSVC: its lack of two-phase name resolution.