c++ - Convert STL map into a struct -
i have std::map<string, double>
members like:
x = [{"n", 200}, {"sigma", 1.0}, {"t", .2}]
now, given struct foo y
:
struct foo { int n; double t; };
can programmatically map key/value pairs x -> y
without writing custom class each x -> y
type mapping? note that:
x["sigma"]
not iny
, i.e. mapping not one-to-one- the type of
y.n
int whilex["n"]
double.
i suspect answer no, unless trickery done @ compile time.
edit: may not clear i'm looking for. pseudo-code version example like:
if("n" in x) -> y.n = x["n"]; if("t" in x) -> y.t = x["t"];
or programmatically:
for key in y: if (key in x) -> y.key = x[key]
no. c++ has no concept of reflection. @ compile time, there's no "foo::n"
string anymore. compiler has converted occurances of foo::n
in source code 0 byte offset within foo
objects. also, cannot enumerate class members @ compile time.
Comments
Post a Comment