Is it possible to define (in a simple way, possibly re-using std container) an "associative std::tuple
", or said in other words a "variadiac std::map
".
Something like this (this interface is just to explain, other possible interfaces are welcome):
AssociativeTuple<std::string> at; // std:string is the key type
at.insert<float>("my_float", 3.14);
at.insert<int>("my_int", 42);
at.insert<std::string>("my_string", "hello world!");
assert(get(at, "my_float") == 3.14);
assert(get(at, "my_int") == 42);
assert(at["my_string"] == "hello world!");
assert(std::is_same<at.type_of("my_float")::type, float>)
for (auto it : at) { std::cout << it.first << " = " << it.second; }
Other desirable constraints:
- The key and the values (and also their type) are known at run time
- Access performance,
get
should be fast - The user don't have to remember the type associated to a key
My real problem is just with value of float
/int
type (and what I am doing is to store in everything in a std::map<std::string, float>
and converting to int
when necessary), but a general solution is desirable. In my real case the keys are always std::string
.
Aucun commentaire:
Enregistrer un commentaire