【发布时间】:2013-07-19 09:49:42
【问题描述】:
我在类特化中声明一个不完整的结构并稍后定义它时遇到问题。
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; };
^
【问题讨论】:
标签: c++ specialization incomplete-type