0

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
  • You can't do it with a struct but you can create such a data structure using user defined types. There are a bunch of json libraries that do this for you. Commented Apr 27, 2016 at 11:29

1 Answer 1

1

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. :)

Sign up to request clarification or add additional context in comments.

2 Comments

is it not possible with Variadic templates? Our library usage is very limited and we are using C++11 compiler
No. Variadic templates are a compile-time construct that allow you to create class/function templates with variable numbers of template arguments.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.