我有一个用例的以下函数:
template<size_t state_dim, size_t action_dim>
class agent {
// [...]
/**
* @brief get_plugin Get a pluging by name
*/
template<typename T>
inline T<state_dim, action_dim>* get_plugin() const {
const string plugin = T<state_dim, action_dim>().name();
for(size_t i = 0; i < this->_plugins.size(); i++)
if(this->_plugins[i].first == plugin)
return static_cast<T<state_dim, action_dim>*>(this->_plugins.at(i).second);
return nullptr;
}
// [...]
}
// a possible usecase
auto sepp = instance.get_plugin<plugin_SEP>();
但我收到以下错误:
error: 'T' is not a template
inline T<state_dim, action_dim>* get_plugin(const string& plugin) const {
^
error: 'T' is not a template
return static_cast<T<state_dim, action_dim>*>(this->_plugins.at(i).second);
^
error: missing template arguments before '>' token
auto sepp = instance.get_plugin<plugin_SEP>();
^
error: expected primary-expression before ')' token
auto sepp = instance.get_plugin<plugin_SEP>();
^
我在这里缺少什么?
答案 0 :(得分:2)
1.您需要声明T
是template template parameter,否则您不能使用模板参数(实例化)它。
template <template <size_t, size_t> class T>
inline T<state_dim, action_dim>* get_plugin(const string& plugin) const {
2.您需要插入关键字template
来调用成员函数模板get_plugin
。
auto sepp = instance.template get_plugin<plugin_SEP>();
有关详细信息,请参阅Where and why do I have to put the “template” and “typename” keywords?。