Tuesday, 15 January 2013

c++ - mpl sequence and recursive code generation -



c++ - mpl sequence and recursive code generation -

lets have mpl sequence types of length n (e.g boost::variant<int,string,double> , sequence of types boost::variant::types ):

i recursively generate next code every possible index. exactly if statements run till w==n

void make(int w){ if(w == 0){ typename boost::mpl::at_c<types,0>::type t; // code }else if (w==1){ typename boost::mpl::at_c<types,1>::type t; // code }... . . . }else if(w==n){ typename boost::mpl::at_c<types,2>::type t; // code }else{ // runtime error } }

how can accomplish this?

as general rule of thumb, since you're trying things @ compiletime, recursion compile time too. implies you'll need static (type) function implements recursion , stop-condition.

if want accomplish code similar pseudo-code show, can utilize boost fusion, contains algorithms mimick familiar runtime algorithms, boost::fusion::for_each.

fusion has associative collections (boost::fusion::map<...>) might fit utilize case.

c++ boost recursion metaprogramming

No comments:

Post a Comment