why using a template as a parameter in the STL map template is refused?

16,037

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_;
Share:
16,037
Joy
Author by

Joy

.Net Developer

Updated on June 04, 2022

Comments

  • Joy
    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
      leftaroundabout about 12 years
      What 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
      Joy about 12 years
      well 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
      leftaroundabout about 12 years
      As I said, that's not possible this way. Make message_typeA, message_typeB derived classes of messegetype_base, then you can use a container with e.g. std::unique_ptrs 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
      Joy about 12 years
      so can I make a container with messagetype_base then when creating the objects I use messagetype_base myMsg= new message_typeA() ??
    • leftaroundabout
      leftaroundabout about 12 years
      Almost. It has to be a container with pointers to messagetype_base, it is for safety and convenience reasons that I would recommend std::unique_ptr.
    • Joy
      Joy about 12 years
      why std::unique_ptr specifically ? what's wrong with a std::auto_ptr or even boost::shared_ptr !
    • leftaroundabout
      leftaroundabout about 12 years
      Read the Wikipedia article.
  • Joy
    Joy about 12 years
    how 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
    Joy about 12 years
    thanks 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 !