继承自专业化的类?

发布于 2024-12-20 10:52:34 字数 1096 浏览 4 评论 0原文

这是有效的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

記憶穿過時間隧道 2024-12-27 10:52:34

要从某个类型继承,该类型必须是完整的。稍微重新安排一下就可以解决问题:

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:

基类型说明符表示的类型应是不是不完全定义的类的类类型;对于正在定义的类,此类称为直接基类

§9.2/2:

类在类说明符的结束 } 处被视为完全定义的对象类型(或完整类型)。

To inherit from a type, that type must be complete. A little rearranging solves things:

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();
}

Token standard quotes:

C++11, §10/2:

The type denoted by a base-type-specifier shall be a class type that is not an incompletely defined class; this class is called a direct base class for the class being defined.

§9.2/2:

A class is considered a completely-defined object type (or complete type) at the closing } of the class-specifier.

紧拥背影 2024-12-27 10:52:34

10/2:

由基类型说明符表示的类型应是不是不完全定义的类的类类型

这是 MSVC 缺陷的一种表现:它缺乏两阶段名称解析。

10/2:

The type denoted by a base-type-specifier shall be a class type that is not an incompletely defined class

It is one manifestation of the bug of MSVC: its lack of two-phase name resolution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文