dariush dariush - 1 month ago 6
C++ Question

error: 'T' is not a template

I have got a following function with a use case:

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>();


But I get the following errors:

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>();
^


What am I missing here?

Answer

1.You need to declare that T is a template template parameter, otherwise you can't use(instantiate) it with template arguments.

template <template <size_t, size_t> class T>
inline T<state_dim, action_dim>* get_plugin(const string& plugin) const {

2.You need to insert keyword template for calling the member function template get_plugin.

auto sepp = instance.template get_plugin<plugin_SEP>();

See Where and why do I have to put the “template” and “typename” keywords? for more details.

Comments