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:

  1. x["sigma"] not in y, i.e. mapping not one-to-one
  2. the type of y.n int while x["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

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -