3

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?

dariush
  • 2,780
  • 2
  • 19
  • 35
  • 1
    By `typename T` you yourself said that `T` is a *type*. Yet later you are trying to use it as a *template*. Types and templates are two completely different things. Why are you trying to use type name as a template name? – AnT Oct 27 '16 at 06:24

1 Answers1

2

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.

Community
  • 1
  • 1
songyuanyao
  • 147,421
  • 15
  • 261
  • 354