Lar*_*ars 5 c++ templates using member-functions c++11
在基类中调用非模板化成员函数时,可以将其名称导入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>();
}
Run Code Online (Sandbox Code Playgroud)
我知道明确地为基类添加前缀
A<T>::template f<T2>();
Run Code Online (Sandbox Code Playgroud)
工作正常,但问题是:是否有可能没有和使用简单的使用声明(就像它f不是模板函数的情况一样)?
万一这是不可能的,有谁知道为什么?
Ben*_*igt 10
这是有效的(双关语): this->template f<T2>();
那样做
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>();
}
};
Run Code Online (Sandbox Code Playgroud)
为什么using不依赖于模板的模板函数非常简单 - 语法不允许在该上下文中使用所需的关键字.