4

I have struct like :

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 is 7
     > data_type;


   // for access by 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 I may create this structure using BOOST preprocessors? For me known only type_A, type_B, ..., type_X name of types.

It's need me, because I must create a lot of structures like that, only changing type_A, type_B, ... types.

In common case, can I use boost preprocessor array or set?

1 Answer 1

7

You could do it like 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 to the maNamespace parameter. You can of course use this argument in any other way (it's just passed through verbatim), such as hardcoding N into the macros (and passing a dummy in the argument), or encoding even the identifier data in there, etc.

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

1 Comment

Note: let's not forget to #undef the macros once we are done with them; there is no need to pollute the global namespace for our clients :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.