c++ - Using template parameter as argument in a template -
c++ - Using template parameter as argument in a template -
this question has reply here:
where , why have set “template” , “typename” keywords? 5 answersi trying implement simple plugin manager using concepts found in this answer. implementing template can instantiate different managers plugins implementing different interfaces.
however, can't compile.
here extract demonstrates problem:
#include <map> #include <string> template<typename interfacetype> class pluginmanager { public: // map between plugin name , mill function typedef std::map<std::string, interfacetype*(*)()> map_type; static interfacetype *createinstance(std::string const& pluginname) { map_type::iterator iter = map().find(pluginname); if (iter == map().end()) homecoming 0; homecoming iter->second(); } protected: static map_type & map() { static map_type map; homecoming map; } }; class myinterface {}; pluginmanager<myinterface> myinterfacepluginmanager; int main(int argc, char *argv[]) { } when trying compile it, happens:
$ g++ pimgr_bug.cpp pimgr_bug.cpp: in static fellow member function ‘static interfacetype* pluginmanager<interfacetype>::createinstance(const std::string&)’: pimgr_bug.cpp:12: error: expected ‘;’ before ‘iter’ pimgr_bug.cpp:13: error: ‘iter’ not declared in scope pimgr_bug.cpp:15: error: ‘iter’ not declared in scope it seems related definition of map_type: if alter map value type concrete class compiles fine, value type defined interfacetype*(*)() or indeed related interfacetype @ all, not work. map supposed hold mapping between plugin name , pointer corresponding mill function.
i missing basic understanding of template syntax!
surely possible create map within template holds type defined 1 of template arguments?
i using gcc 4.4.7 , unfortunately cannot utilize c++11 (if that's relevant).
thanks!
map_type::iterator dependent name, because map_type depends on template parameter (interfacetype). subsequently, compiler doesn't assume map_type::iterator names type unless explicitly so*.
therefore, write
typename map_type::iterator iter = map().find(pluginname); and should compile fine.
* or name lookup finds one, doesn't apply here.
c++ templates
Comments
Post a Comment