Boost logo

Boost Users :

Subject: Re: [Boost-users] [fusion] memory layout of fusion::vector vs boost::tuple/boost::array
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2010-10-07 18:33:16


On Wed, Oct 6, 2010 at 10:54 PM, alfC <alfredo.correa_at_[hidden]> wrote:
>
>
> On Oct 6, 5:33 pm, Joel de Guzman <j..._at_[hidden]> wrote:
>> On 10/7/2010 3:53 AM, alfC wrote:
>>
>> > Hi,
>>
>> >   I am trying to convert a boost::array into a fusion::vector.
>> > reinterpreting the memory from boost::array. It works for tuples but
>> > not fusion::vector, why is that. Is there a way to make it work? ( I
>> > was hoping that the memory layout is the same to make conversion from
>> > one to the other very easy.)
>>
>> Don't do that. It will *never* be guaranteed to work even if it works
>> now. The memory layout of fusion::vector is an internal implementation
>> detail and can change anytime.
>>
>
> It is not only not guaranteed, it does not work at present. Any two
> cents why is that fusion::vector may (do?) implement a non obvious
> memory layout?
>
> I solved the problem of array<double, N> to vector<double ...>:
> I found the feature of the library that allows conversion from
> array<double, N> to vector<double*N ... >. Just by including
> #include <boost/fusion/adapted/boost_array.hpp>
> #include <boost/fusion/include/boost_array.hpp>
>
> boost::array<double, 3> ba={{1.0, 2.1, 3.2}};
> vector<double, double, double> bfv1(ba); //works.
>
> I still can the other way around.
> boost::array<double, 3> ba0(bfv1); //doesn't work
> ideas?
>
> great, I also managed to solve the original problem on how to convert
> from
> boost::array<double, N> to
> fusion::vector<quantity<T1>, quantity<T2>, ... quantity<TN> >
> thanks to your other posting suggesting using fusion::transform
>
> quantity<T> can be constructed from a double with
> quantity<T>::from_value(double)
>
> The line of code that does this is
>
> typedef boost::fusion::vector<quantity<si::length>,
> quantity<si::time>, quantity<si::mass> > vector_of_q;
> vector_of_q fvq(
>        transform(
>                ba, // boost::array<double, 3> // incld"/fusion/array.hpp"!!
>                vector_of_q(),
>                from_value()
>        )
> );
>
> where "from_value" is defined by
>
> struct from_value{ // binary fusion transformation
>        template <typename SignatureT> struct result;
>        template <typename Q>
>        struct result<from_value(double const&, Q const&)>{
>                typedef Q type;
>        };
>        template<typename Q>
>        Q operator()(double const& value, Q const&) const{
>                return Q::from_value(value);
>        }
> };
>
> I had to use the *binary* version of transform because the type of the
> result depends on the destination fusion::vector.
>
> The other way around from vector<quantity<T1> ... quantity<TN> > to
> array<double, N> should be easier (because the destination type is
> always double by using the quantity<T>::value() ) but I didn't managed
> because I didn't find the way to convert from vector<double*N> to
> array<double, N>. ideas?
>
> Thank you for making fusion, it is an incredible library once you
> realize what it is for.
> Alfredo

You do know that based on this:
  http://www.boost.org/doc/libs/1_44_0/libs/fusion/doc/html/fusion/adapted/boost__array.html
boost::array is already adapted as a fusion container and can be used
directly, as soon as you include the proper hpp that is?


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