c++ - BOOST_PP_REPEAT with array -
i have struct :
struct e1 { typedef boost::tuple< boost::optional< n::type_a >, // n - namespace boost::optional< n::type_b >, ................... boost::optional< n::type_x > //arbitrary number of, maximal 7 > data_type; // access name boost::optional<n::type_a> const& type_a() const { return boost::get<0>(data); } boost::optional<n::type_b> const& type_b() const { return boost::get<1>(data); } ..................................... boost::optional<n::type_x> const& type_x() const { return boost::get<2>(data); } data_type data; };
q: how may create structure using boost preprocessors? me known type_a, type_b, ..., type_x name of types.
it's need me, because must create lot of structures that, changing type_a, type_b, ... types.
in common case, can use boost preprocessor array or set?
you this:
#define types (type_a)(type_b)(type_x) #define generate_tuple(mar, manamespace, maindex, matype) \ boost_pp_comma_if(maindex) boost::optional<manamespace :: matype> #define generate_getter(mar, manamespace, maindex, matype) \ boost::optional<manamespace :: matype> const& matype () const { return boost::get<maindex>(data); } struct e1 { typedef boost::tuple< boost_pp_seq_for_each_i(generate_tuple, n, types) > data_type; boost_pp_seq_for_each_i(generate_getter, n, types) data_type data; };
the n
argument corresponds manamespace
parameter. can of course use argument in other way (it's passed through verbatim), such hardcoding n
macros (and passing dummy in argument), or encoding identifier data
in there, etc.
Comments
Post a Comment