Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I implement a map with different data types as values?

I want to put two (not more) different data types as values into a map as shown in the following example:

typeX A, B, ...;
typeY Z, Y, ...;

void func (typeX) { ... }
void func (typeY) { ... }

std::map <std::string, what_to_put_here??> map;
map["a"] = A;
map["z"] = Z;
...

std::vector<std::string> list;
// This list will be something like "a", "y", ...

for (unsigned int i = 0; i < list.size(); ++i)
    func( map[list[i]] )

Obviously this doesn't work, as the map will only accept one data type of value. However, when looping over list, the call to func() should be unambiguous since the type of map[list[i]] is known.

I want to avoid explicit casting or type checking, i.e. something like this:

if (typeid( map[list[i]] ).name() == "typeX")
    func( map[list[i]] )
else if (typeid( map[list[i]] ).name() == "typeY")
    func( map[list[i]] )

Is this possible? Again, it will be limited to only two different data types.

like image 876
user3240855 Avatar asked Jan 27 '14 15:01

user3240855


4 Answers

You want to use boost::variant:

std::map <std::string, boost::variant<typeX, typeY>>
like image 167
Paul Evans Avatar answered Nov 09 '22 12:11

Paul Evans


Are typeX and typeY subclasses of a typeBase class ? If so, you could do a std::map<std::string,typeBase*> to store both typeX* and typeY* in the map.

like image 27
rom1504 Avatar answered Nov 09 '22 12:11

rom1504


With some metaprogramming you can easily build an heterogenous map which can store any type from a given set of types. Here is an example which does this, without type erasure nor the need to visit the values.

like image 1
Julien Avatar answered Nov 09 '22 11:11

Julien


One way to implement a multi-type map is by using the nifty features of std::tuple in C++11, which allows access by a type key. You can wrap this to create access by arbitrary keys. An in-depth explanation of this (and quite an interesting read) is available here:

https://jguegant.github.io/blogs/tech/thread-safe-multi-type-map.html

like image 1
johnbakers Avatar answered Nov 09 '22 12:11

johnbakers