Boost logo

Boost Users :

Subject: Re: [Boost-users] [fusion] more trouble with transform
From: Gennadiy Rozental (rogeeff_at_[hidden])
Date: 2011-11-12 07:08:52


Ovanes Markarian <om_boost <at> keywallet.com> writes:

> You need to make a list of references to Bs: 
>    B b1, b2;
>    auto z1 = fusion::make_list( ref(b1), ref(b2) );
>    auto z2 = fusion::as_list( fusion::transform( z1, make_c() ) );

This one indeed works, but what I am really after is something like this:

auto z1 = fusion::make_list( B(), B() );
auto z2 = fusion::as_list( fusion::transform( z1, make_c() ) );

I know first line will move constructed instances into list z1. Next I need to
mutate them and adding boost::ref around B() is impossible. In reality my use
case somewhat more complicated. Something like this:

#include <boost/fusion/container/list/list.hpp>
#include <boost/fusion/container/list/convert.hpp>
#include <boost/fusion/container/generation/make_list.hpp>
#include <boost/fusion/algorithm/transformation/transform.hpp>
namespace fusion=boost::fusion;

struct C {};

struct B {
    C make_c() { return C(); }
};

class A {
public:
    B make_b() const { return B(); }
};

//____________________________________________________________________________//

struct make_b {
    template<typename Sig> struct result;

    template <typename T>
    struct result<make_b(T const&)>
    {
        typedef B type;
    };
    template <typename T>
    struct result<make_b(T)>
    {
        typedef B type;
    };

    template <typename T>
    B
    operator()(T const& t) const { return t.make_b(); }
};

struct make_c {
    template<typename Sig> struct result;

    template <typename T>
    struct result<make_c(T&)>
    {
        typedef C type;
    };
    template <typename T>
    struct result<make_c(T)>
    {
        typedef C type;
    };

    template <typename T>
    C
    operator()(T& t) const { return t.make_c(); }
};

template<typename T>
void
do_something( T const& t )
{
}

int
main()
{
    auto z1 = fusion::as_list( fusion::transform( fusion::make_list( A(), A() ),
make_b() ) );

    size_t size = 1; // I calculate size here, irrelevant to the question
    while( size-- > 0 )
        do_something( fusion::as_list( fusion::transform( z1, make_c() ) ) );

    return 0;
}

Same problem.

Gennadiy


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