【发布时间】:2015-11-17 00:00:16
【问题描述】:
考虑以下几点:
template<typename X>
struct Z {};
struct A
{
using Z = ::Z<int>;
struct B : Z
{
using C = Z;
};
};
这编译得很好。好的。但是现在在Z中添加另一个参数:
template<typename X, typename Y>
struct Z {};
struct A
{
template<typename X>
using Z = ::Z<X, int>;
struct B : Z<B>
{
using C = Z<B>; // error: too few template arguments for class template 'Z'
};
};
好的,也许在派生嵌套类B 时,类A 中模板别名Z 的定义是可见的,但在其主体内不可见,这会触发自Z 的全局定义以来的错误有两个参数。
但是为什么在第一种情况下的行为不同,Z 只是A 中的类型别名?
最后,将A 设为模板:
template<typename X, typename Y>
struct Z {};
template<typename T>
struct A
{
template<typename X>
using Z = ::Z<X, int>;
struct B : Z<B>
{
using C = Z<B>;
};
};
现在错误消失了。 为什么?
(在 Clang 3.6 和 GCC 4.9.2 上测试)
【问题讨论】:
标签: c++ c++11 language-lawyer nested-class template-aliases