Re: Metaprogramming woes

"White Wolf"
Hi Dave,
Hi Atilla, In future please post your questions to the boost-users list so that the whole community can benefit.
Well, the author of _2 can't anticipate all the nested values you
might be using. _2 is not magic; it's just a type.
// !! untested code !!
#include
::type sum;
// sum::value contains the value you're interested in.
Yes, that's a synonym for fold if you have a version of the MPL that includes accumulate.
Finally I guess I will have to use transform in some way, because I need a list which has an mpl::pair (if there is one)
http://www.boost.org/libs/mpl/doc/refmanual/pair.html
of the original type and the sum of all nr_'s before it.
Seems like it's still a job for fold. In this case I'd use mpl::vector because lists can only be push_front-ed and you probably don't want to end up with your elements in "reverse" order. typedef mpl::fold< some_list , mpl::pair< // initial state mpl::vector0 // initial sequence , mpl::size_t<0> // initial sum > , mpl::pair< // the state at each step is a pair containing mpl::push_front< // added to the front of... mpl::first<_1> // the sequence from the previous step , mpl::pair< // a new element, a pair containing _2 // the original type , mpl::plus< // and the sum of mpl::second<_1> // the sum from previous step , get_nr_<_2> // the ::nr_ of the original type > > > , mpl::plus< // and the sum of mpl::second<_1> // the sum from previous step , get_nr_<_2> // the ::nr_ of the original type > >
::type pair_seq_sum;
typedef pair_seq_sum::first new_sequence;
So this is pretty verbose using lambdas because the sum is repeated,
among other things. You might do better to build a custom metafunction
that gets you the new state:
template
Where does n+2 come from?
?? that doesn't look anything like what you just described.
Sheesh. The above is still a job for fold.
I suggest you start with a base type:
template <class Derived>
struct base
{
enum { sum = 0 };
void get(); // never used.
};
then build layers:
template
participants (1)
-
David Abrahams