Is there a way to have a heterogeneous
map implementation as the below with the following conditions please:
- The types are known at run time as well as the read keys.
- Almost zero overhead e.g. not
boost::any
that's slow for my purposes. - No ugly long macros.
class special_map;
class buffer
{
public:
~buffer() = default;
buffer(const special_map& map) : m_map(map) {};
template<typename T>
void read(const std::string& name, T& value)
{
map.read<T>(name, value);
return void();
}
private:
special_map m_map;
};
Can I use:
void*
withreinterpret_cast
please? How? Is it safe for the client/user?- can I use
boost::hana
orboost::fusion
.
Thanks very much? (Please plz see conditions)
Here is an implementation that internally uses
std::map
(or any map implementation of your choice) for every typeT
that we want to be able to store. From the comments, I read that it is the case that we know what types we want to store. I would expect the overhead of this implementation to be roughly the same as the underlying map implementation, but it is not my job to test that. There are no funky pointer casts or use of std::variant et al in this implementation:So to use it, you have to list all the types that it should be able to store, e.g.
If you try to use it with a type not listed in that list, I would expect you will get a funny compilation error, so you will know that you have to add it there :-) For example, if I try to read a bool from
myMap
I get the error