Boost logo

Boost :

Subject: [boost] [mpl] struct definition and mpl sequence in one shot?
From: Adam Romanek (romanek.adam_at_[hidden])
Date: 2014-10-03 02:27:38


This is a re-post from the boost-users mailing list.

I have a problem and I'm not sure how to approach it. Currently my code
looks like this:

struct Definition {
   typedef boost::mpl::int_<5> A;
   typedef boost::mpl::int_<3> B;
   typedef boost::mpl::int_<6> C;
   typedef boost::mpl::int_<1> D;

   typedef boost::mpl::vector<A, B, C, D> Seq;

Here, the number N in mpl::int_< N > denotes some arbitrary decimal
number. Then some other code calculates the sum of these numbers up to
the type defined by "key", e.g. for Definition::D, the sum is 5 + 3 + 6
(A + B + C). This needs to be done at compile-time. That's why I use
mpl::vector and some appropriate meta-programming.

I'm wondering whether it would be possible to provide such struct
definition without the need to repeat type names in the mpl::vector for
Seq type. In other words, I'd probably need a bunch of macros which
would allow me to write code like this:

struct Definition {
   FIELD(A, 5);
   FIELD(B, 3);
   FIELD(C, 6);
   FIELD(D, 1);
   GEN_SEQ() // if needed

And then Definition::A would still refer to boost::mpl::int_<5>, or
would at least allow me to access the boost::mpl::int_<5> somehow, and
Definition::Seq would give me the appropriate MPL sequence.

Of couse this is just my imagination. The code might look different, I'm
just looking for options.

Thanks in advance.
Adam Romanek

Boost list run by bdawes at, gregod at, cpdaniel at, john at