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?
template <typename T> ... This means exactly the same thing as the previous instance. The typename and class keywords can be used interchangeably to state that a template parameter is a type variable (as opposed to a non-type template parameter).
To instantiate a template function explicitly, follow the template keyword by a declaration (not definition) for the function, with the function identifier followed by the template arguments. template float twice<float>(float original); Template arguments may be omitted when the compiler can infer them.
Function templates are similar to class templates but define a family of functions. With function templates, you can specify a set of functions that are based on the same code but act on different types or classes. The following function template swaps two items: C++ Copy.
This is a very important concept to understand when dealing with templates in C++: templates don't exist at runtime. All templates are compiled to real types at compile time.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With