使 class 模板化强制在继承构造函数中重复基础 class 模板参数
Making class templated forces repeating base class template params in inheriting constructor
我最近将 class 从模板化更改为非模板化,并发现在编写 using 声明以从模板化基础 class 继承构造函数时,我不能再省略模板参数。只要我的 class 不是模板化的,我就可以省略参数,一旦是我就不能。在下面的可编译片段中,bar
代表之前的 class,buzz
代表之后的 class。我已经测试了 GCC 5.2 和 Clang 3.7,它们具有相同的行为。这是编译器错误还是标准?
#include <iostream>
template<class A, class B>
struct foo {
foo(int x) {
std::cout << x << std::endl;
}
};
struct bar : foo<bar, short> {
using foo::foo; // this appears legal
// using foo<bar, short>::foo; // this works too, thought I would need it
};
template<class X>
struct buzz : foo<buzz<X>, short> {
//using foo::foo; // no longer legal for some reason
using foo<buzz<X>, short>::foo; // now do actually need this
};
int main() {
bar x(3);
buzz<float> y(5);
return 0;
}
这是标准的。
N4140 [temp.dep]/3:
In the definition of a class or class template, if a base class depends on a template-parameter, the base class
scope is not examined during unqualified name lookup either at the point of definition of the class template
or member or during an instantiation of the class template or member.
对于 buzz
,基础 class 取决于模板参数,因此 foo
非限定查找不检查其范围。这就是为什么您需要合格的查找。
我最近将 class 从模板化更改为非模板化,并发现在编写 using 声明以从模板化基础 class 继承构造函数时,我不能再省略模板参数。只要我的 class 不是模板化的,我就可以省略参数,一旦是我就不能。在下面的可编译片段中,bar
代表之前的 class,buzz
代表之后的 class。我已经测试了 GCC 5.2 和 Clang 3.7,它们具有相同的行为。这是编译器错误还是标准?
#include <iostream>
template<class A, class B>
struct foo {
foo(int x) {
std::cout << x << std::endl;
}
};
struct bar : foo<bar, short> {
using foo::foo; // this appears legal
// using foo<bar, short>::foo; // this works too, thought I would need it
};
template<class X>
struct buzz : foo<buzz<X>, short> {
//using foo::foo; // no longer legal for some reason
using foo<buzz<X>, short>::foo; // now do actually need this
};
int main() {
bar x(3);
buzz<float> y(5);
return 0;
}
这是标准的。
N4140 [temp.dep]/3:
In the definition of a class or class template, if a base class depends on a template-parameter, the base class scope is not examined during unqualified name lookup either at the point of definition of the class template or member or during an instantiation of the class template or member.
对于 buzz
,基础 class 取决于模板参数,因此 foo
非限定查找不检查其范围。这就是为什么您需要合格的查找。