Say I have a class / struct like below:
struct A {
uint32_t a;
uint8_t b;
uint16_t c;
};
And I have a set of strings that associate to each member of A (could be of different integer types, but not non-integer types such as strings), e.g.
"field1" -> A::a
"field2" -> A::b
"field3" -> A::c
Assume that there is always a 1:1 mapping between the strings and members. Is there an elegant way to map each string to each member using something like std::unordered_map? I want to be able to read and write to each field using the strings as keys, e.g.
A a {1,2,3};
mymap["field1"] = 4; // a.a = 4
mymap["field2"] = 5; // a.b = 5
auto c = mymap["field3"]; // c = a.c = 3
I'm using C++11/14 and can't use boost.
Some more information on the context of this question:
The struct A mentioned in the question are settings of the program. They are hardware configuration parameters and my software program is used to simulate hardware behaviours. These settings/configurations are script-generated structs like above. We read / write these struct members and because the amount of these settings/configurations are so many (a few thousands), it would be convenient to be able to access them by their names as well. This is how & why I want to associate each member with a string. Whether it is subscripting or a function to access the corresponding members does not really matter, but there is such a 1:1 mapping between a string (name of the setting) and the generated struct member. And as I mentioned in the question, these members are of different integer types.