# Heterogeneous map

This topic is 2914 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm faced with a problem.

I'd like to have something along the lines of a heterogeneous map class, eg:

 heterogeneous_map map; map.set<std::string>("name", "Chris"); map.set<int>("age", 23); std::string name = map.get<std::string>("name"); int age = map.get<int>("age"); 

This isn't particularly hard to achieve with a std::map<std::string, boost::any>, the problem arises because I need to be able to serialize and de-serialize these heterogeneous maps. I'm using Boost::Serialization, which works very well for most things I've tried so far, but it has no support for serializing a boost::any object. What can I do?

##### Share on other sites
Actually, that may not be necessary.

There is Boost.PropertyTree that has basically that exact interface, and furthermore, it already works with the serializer!

 boost::property_tree::ptree pt; pt.put<std::string>("name", "Chris"); pt.put("age", 23); std::ofstream ofs("out.xml"); boost::archive::xml_oarchive xml(ofs); xml << BOOST_SERIALIZATION_NVP(pt); 

Is this a good solution?

Edit: Eh, on second thought. Ptree doesn't seem to support anything but basic C types and std::string. Is there a way to expand this?

##### Share on other sites
How are you planning to remember which type to use for what data? If there is a set protocol, maybe it's possible to just use a struct? :

This kind of thing is fighting the language to an extraordinary degree. You might also consider using a language that actually supports (built-in) what you're trying to do.

Also, this isn't really a game-specific question so I'm moving it to General Programming.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 18
• 13
• 9
• 9