Boost logo

Boost Users :

Subject: Re: [Boost-users] Generating a large number of class member variables: PP vs mpl
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-01-20 13:59:01


AMDG

Hicham Mouline wrote:
> I just realized you can instantiate fusion vector objects (forgot fusion is about runtime too)
>
> So I have this example:
>
> struct A {
> explicit A(int) {}
> };
>
> struct B {
> explicit B(int) {}
> };
>
> struct C {
> explicit C(int) {}
> };
>
> class Derived1 {
> public:
> Derived1 () : members( A(5), B(5), C(5) ) {}
> private:
> const boost::fusion::vector< A, B, C > members;
> };
>
> All members are constructed with the same argument(s) : 5
>
>
> Now:
> 1. I may have 20 members in vector.... Is there a way not to explicitly construct them in the Derived1 ctor
>

You need to create a fusion sequence that consists of the same arguments
repeated many times.

template<class T>
struct constant {
    constant(const T& v) : value(v) {}
    T value;
    typedef T result_type;
    template<class U>
    T operator()(const U&) const { return(value); }
};

...

members(boost::fusion::transform_view(boost::mpl::range_c<0, 20>(),
constant<int>(5)))

> 2. Derived3 and Derived7 have the same vector of members. Is there a way, inside Derived3 and 7, to take the vector from Derived1?
>

fusion containers can be copied from each other. The copy will be
done member wise.

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net