Boost logo

Boost Users :

Subject: Re: [Boost-users] Selecting one element per fusion sequence using mpl::int_s
From: TONGARI J (tongari95_at_[hidden])
Date: 2014-06-29 01:12:08


2014-06-29 8:38 GMT+08:00 gsermaid <linuxfever_at_[hidden]>:

> Dear all,
>
> I am trying to create a fusion sequence whose elements are non-const
> references to the elements of given set of sequences. In particular, for a
> given sequence of integral constants, say vector< int_<2>, int_<1> >, and
> two fusion sequences, say X = vector< X0, X1, X2, X3 > and Y = vector< Y0,
> Y1, Y2 >, I would like to create the following sequence
>
> R = vector< X2&, Y1& >, such that at<0>(R) == at<2>(X) and at<1>(R) ==
> at<1>(Y).
>
> I am using fusion::transform to achieve the above but the transform
> function takes the sequences as const references so I end up with R =
> vector<const X2&, const Y1&>, which is not what I want. Below is my code;
> any ideas on how to get a sequence of non-const references would be greatly
> appreciated. Thanks for reading!
>
> #include <boost/fusion/include/vector.hpp>
> #include <boost/fusion/include/transform.hpp>
> #include <boost/fusion/sequence/io/out.hpp>
> #include <boost/fusion/include/at.hpp>
> #include <boost/mpl/assert.hpp>
> #include <boost/type_traits.hpp>
>
> #include <iostream>
>
> using namespace boost::fusion;
> using boost::mpl::int_;
>
> using my_ints = vector< int_<2>, int_<1> >;
> using my_x = vector<int, double, unsigned, float>;
> using my_y = vector<unsigned, float, long>;
> using joined = vector<my_x, my_y>;
>
>
> struct my_functor {
>
> template <class I, class Seq>
> typename result_of::at<Seq, I>::type operator()(I& i, Seq& seq) const {
> return boost::fusion::at<I>(seq);
> }
> };
>
> int main() {
>
> my_ints ints;
> my_x x(-2, 3.5, 15, -1.3);
> my_y y(4, 10.1, 100);
> joined jv(x, y);
>
> auto tv = transform(ints, jv, my_functor());
> std::cout << tv << std::endl; // prints (15 10.1)
>
> // these two fail to compile because the first argument is
> const-qualified
> BOOST_MPL_ASSERT(( boost::is_same< decltype(at_c<0>(tv)), unsigned& > ));
> BOOST_MPL_ASSERT(( boost::is_same< decltype(at_c<1>(tv)), float& > ));
> }
>

For now, you can take this file:
https://github.com/jamboree/fusion/blob/develop/include/boost/fusion/support/ref.hpp

Include it, and use it as:
auto tv = transform(ints, boost::ref(jv), my_functor());

HTH



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