[英]C++ calling template method of template base class
我有以下情况:
一个可变参数模板类,它继承“自身”来解析可变参数模板参数。 可变参数模板类具有两个模板方法。 在这些方法中,我想调用基础模板类的模板方法。 对于set方法,它似乎起作用。
template<class T, class ... R>
class ValueProvider : ValueProvider<R...>{
public:
T value;
const std::type_info& type = typeid(T);
ValueProvider()
{}
template<class G>
inline G get(){
if(this->type.hash_code() == typeid(G).hash_code())
return this->value;
else{
//----- interesting part -----
return ValueProvider<R...>::get<G>(); /*<- compile ERROR: expected primary-expression before ‘>’ token*/
return ValueProvider<R...>::get(); /*<- compile ERROR: no matching function for call to ‘ValueProvider<int, char>::get()’*/
return ValueProvider::get<G>(); /*<- runtime ERROR (infinit recursion)*/
}
}
template<class G>
void set(G p){
if(this->type.hash_code() == typeid(G).hash_code())
this->value = p;
else
ValueProvider<R...>::set(p);
}
};
template<class T>
class ValueProvider<T>{
public:
T value;
const std::type_info& type = typeid(T);
ValueProvider()
{}
template<class G>
inline G get(){
if(this->type.hash_code() == typeid(G).hash_code())
return this->value;
throw "fail";
}
template<class G>
void set(G p){
if(this->type.hash_code() == typeid(G).hash_code())
this->value = p;
else
throw "fail";
}
};
如何调用基类的模板函数?
通常依赖名称的问题:如ValueProvider<R...>
要看R
你必须消除歧义是get
在该范围内是一个模板:
return ValueProvider<R...>::template get<G>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.