why using a template as a parameter in the STL map template is refused?
Solution 1
I'm not sure about this, but you are trying to declare a variable, and that definition has to be fully defined. Trying to use a template doesn't fully define the variable.
You can wrap it in a structure if you want to:
template<typename T>
struct Wrapper
{
typedef std::map<int, T> map_type;
};
Then use it like this:
Wrapper<std::string>::map_type my_wrapped_map;
my_wrapped_map[1] = "Foo";
Solution 2
You're probably not using a C++11 compiler, and this line is invalid:
std::map<int,template<class T>> map_;
It should be
std::map<int,template<class T> > map_;
Notice the space between > >
. Pre-C++11, >>
is always treated as the bit shift operator.
Besides this, what is the code supposed to do? If I'm not mistaken, you should declare your map as
std::map<int,message> map_;
Now, std::map<int,template<class T>> map_;
doesn't really make sense, unless this is a member of another template class, in which case you need
std::map<int,T> map_;
Comments
-
Joy almost 2 years
I wrote this little code
std::map<int,template<class T>> map_; map_.insert(make_pair<int,message>(myMsg.id,myMsg));
but the compiler doesn't seem to get it and displays as an error
template argument 2 is invalid
and when I tried to correct by doing this
template<class T> std::map<int,T> map_;
it displays as an error :
expected primary-expression before 'template' |
error: expected ';' before 'template'
-
leftaroundabout about 12 yearsWhat is it supposed to do? Note that templates must always be resolved at compile-time. It seems you want to build a polymorphic container with them, which is not possible; you must use virtual inheritance for that.
-
Joy about 12 yearswell actually I'm supposed to store different kind of message each type in a map , so I tried to make a global structure by calling it by "std::map<int,template<class T> > map_;" (which doesn't work neither) so class could be message_typeA, message_typeB, etc
-
leftaroundabout about 12 yearsAs I said, that's not possible this way. Make
message_typeA
,message_typeB
derived classes ofmessegetype_base
, then you can use a container with e.g.std::unique_ptr
s to any of these objects. It's a bit more complicated than in dynamic languages (or at least garbage-collected ones), but it does have its benefits (compile-time type safety, good performance...). -
Joy about 12 yearsso can I make a container with messagetype_base then when creating the objects I use messagetype_base myMsg= new message_typeA() ??
-
leftaroundabout about 12 yearsAlmost. It has to be a container with pointers to
messagetype_base
, it is for safety and convenience reasons that I would recommendstd::unique_ptr
. -
Joy about 12 yearswhy std::unique_ptr specifically ? what's wrong with a std::auto_ptr or even boost::shared_ptr !
-
leftaroundabout about 12 yearsRead the Wikipedia article.
-
-
Joy about 12 yearshow do I know which compiler I'm currently using ?? is there any command I need to run ?? btw I'm using codeblocks as an IDE which includes the GCC compiler and GDB debugger from MinGW.
-
Joy about 12 yearsthanks a lot that worked well but I tried to add another field to this struct " typedef std::map<int,T >::iterator map_type_iter; " but it displayed an error and I can't get what's wrong with it !