Is it possible to programmatically create objects of structs and fill the fields, when you have a list of struct names and field values? Say I have to read entry from (JSON)file and fill up my structs. I have value struct names and values but i want to code such that code will loop all structs and fill the in-memory data. may sound wierd, but is there a way?
1 Answer
Nope. The language feature you're looking for is called reflection, and C++ does not have it.
You could build up a std::map<std::string, SomeType>
instead? If SomeType
differs depending on the field, a boost::variant
will allow you to store any of a number of types at any given time (it's basically a tagged union).
Or you could switch to Python. :)
2 Comments
sach
is it not possible with Variadic templates? Our library usage is very limited and we are using C++11 compiler
Lightness Races in Orbit
No. Variadic templates are a compile-time construct that allow you to create class/function templates with variable numbers of template arguments.
struct
but you can create such a data structure using user defined types. There are a bunch ofjson
libraries that do this for you.