当在基类中调用非模板化成员函数时,可以使用以下命令导入其名称using
到派生类中然后使用它。这对于基类中的模板成员函数也可能吗?
只是与using
它不起作用(使用 g++-snapshot-20110219 -std=c++0x):
template <typename T>
struct A {
template <typename T2> void f() { }
};
template <typename T>
struct B : A<T> {
using A<T>::f;
template <typename T2> void g() {
// g++ throws an error for the following line: expected primary expression before `>`
f<T2>();
}
};
int main() {
B<float> b;
b.g<int>();
}
我知道显式地为基类添加前缀,如下所示
A<T>::template f<T2>();
工作正常,但问题是:是否可以不使用和使用简单的 using 声明(就像它的情况一样)f
不是模板函数)?
如果这是不可能的,有谁知道为什么?
这有效(双关语):this->template f<T2>();
So does
template <typename T>
struct B : A<T> {
template <typename T2> void f()
{ return A<T>::template f<T2>(); }
template <typename T2> void g() {
f<T2>();
}
};
Why using
不适用于依赖于模板的模板函数非常简单——语法不允许在该上下文中使用所需的关键字。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)