gpt4 book ai didi

c++ - 定义特殊类的不完整结构

转载 作者:太空狗 更新时间:2023-10-29 23:20:42 25 4
gpt4 key购买 nike

我在类特化中声明不完整的结构并稍后定义它时遇到问题。

struct Foo {
template <bool Y, typename D>
struct Bar {};

template <typename D>
struct Bar<true, D> {
struct Qux;
};

template <typename D>
struct Bar<true, D>::Qux { int x; };
};

此代码在 gcc 中有效,但在 clang 3.3 中失败:

r.cpp:42:26: error: non-friend class member 'Qux' cannot have a qualified name
struct Bar<true, D>::Qux { int x; };
~~~~~~~~~~~~~~^

如果代码是在命名空间范围内编写的(没有 struct Foo),它也可以在 clang 中工作。

另一方面,如果 struct Foo 变成一个模板,如下所示,代码在 gcc-4.9(未发布)中中断,尽管它在 gcc-4.7 中继续工作。

template <typename X>
struct Foo {
template <bool Y, typename D>
struct Bar {};

template <typename D>
struct Bar<true, D> {
struct Qux;
};

template <typename D>
struct Bar<true, D>::Qux { int x; };
};

Clang 失败:

r.cpp:43:26: error: template specialization or definition requires a template parameter list corresponding to the nested type 'Bar<true, type-parameter-1-0>'
struct Bar<true, D>::Qux { int x; };
^
r.cpp:43:26: error: non-friend class member 'Qux' cannot have a qualified name
struct Bar<true, D>::Qux { int x; };
~~~~~~~~~~~~~~^
2 errors generated.

Gcc-4.9 失败并出现类似错误:

r.cpp:43:26: error: too few template-parameter-lists
struct Bar<true, D>::Qux { int x; };
^

最佳答案

看起来您别无选择,只能将该定义放在命名空间范围内(或 Bar 内)。第 9/1 段 (n3337) 说你的代码是非法的:

If a class-head-name contains a nested-name-specifier, the class-specifier shall refer to a class that was previously declared directly in the class or namespace to which the nested-name-specifier refers, or in an element of the inline namespace set (7.3.1) of that namespace (i.e., not merely inherited or introduced by a using-declaration), and the class-specifier shall appear in a namespace enclosing the previous declaration. In such cases, the nested-name-specifier of the class-head-name of the definition shall not begin with a decltype-specifier.

关于c++ - 定义特殊类的不完整结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17757446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com