Hetero Map

In java I have created a generic map whose key is of String type and whose types could be different based on the key value.

Could I create the similar map in C++? Is there any existing map? If not, could you provide some suggestions?

Thanks
You cannot create containers like this in C++, since C++ does not have a "base" type that everything derives from. You would either have to use a void* (ew), or use your own classes that derive from some base class.
I could have sworn that http://boost.org had a library for this but I do not remember its name nor can I locate it in their documentation...

EDIT: Check out boost::any.
Last edited on
That implied I also need to create some primitive type wrappers that extends the base class. Is there any existing maps providing similar capabilities?
For boost::any, you wouldn't need anything special.

std::map<std::string, boost::any> stuff;
Another possible answer is boost::variant.

Whether boost::any or boost::variant is more appropriate depends upon how you access the data.
Thanks a lot for your suggestions. boost::any/boost::variant could solve my issue.
Topic archived. No new replies allowed.