C++ 定义专用类的不完整结构

C++ 定义专用类的不完整结构,c++,specialization,incomplete-type,C++,Specialization,Incomplete Type,我在类专门化中声明一个不完整的结构并在以后定义它时遇到问题 struct Foo { template <bool Y, typename D> struct Bar {}; template <typename D> struct Bar<true, D> { struct Qux; }; template <typename D> struct Bar<true,

我在类专门化中声明一个不完整的结构并在以后定义它时遇到问题

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.
r.cpp:43:26:错误:模板专门化或定义需要与嵌套类型“Bar”对应的模板参数列表
结构条::Qux{intx;};
^
r、 cpp:43:26:错误:非好友类成员“Qux”不能具有限定名称
结构条::Qux{intx;};
~~~~~~~~~~~~~~^
产生2个错误。
Gcc-4.9出现类似错误:

r.cpp:43:26: error: too few template-parameter-lists
     struct Bar<true, D>::Qux { int x; };
                          ^
r.cpp:43:26:错误:模板参数列表太少
结构条::Qux{intx;};
^
struct Foo{
模板
结构条{};
};
模板
结构Foo::Bar{
结构Qux;
};
模板
结构Foo::Bar::Qux{
int x;
};

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

如果类标题名称包含嵌套名称规范,则类规范应指的是 以前直接在嵌套名称规范引用的类或命名空间中声明,或在 该名称空间的内联名称空间集(7.3.1)的元素(即,不仅仅由 使用声明)和类规范应出现在包含先前声明的命名空间中。 在这种情况下,定义的类首名称的嵌套名称规范不得以字母开头 decltype specifier

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.
r.cpp:43:26: error: too few template-parameter-lists
     struct Bar<true, D>::Qux { int x; };
                          ^
struct Foo {
  template <bool Y, typename D>
  struct Bar {};
};

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

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