|
Boost Users : |
Subject: [Boost-users] [fusion] memory layout of fusion::vector vs boost::tuple/boost::array
From: alfC (alfredo.correa_at_[hidden])
Date: 2010-10-06 15:53:05
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.)
#include <boost/fusion/container.hpp>
#include <boost/fusion/include/container.hpp>
#include<boost/array.hpp>
#include<iostream>
#include<boost/tuple/tuple.hpp>
using std::clog; using std::endl;
int main(){
boost::array<double, 3> ba={{1.0, 2.1, 3.2}};
clog << "ba: " << ba[0] << ", " << ba[1] << ", " << ba[2] << endl;
using namespace boost::fusion;
vector<double, double, double> bfv( reinterpret_cast<vector<double,
double, double>&>(ba) ); //doesn't work as expected
clog << "bfv: " << at_c<0>(bfv) << ", "<< at_c<1>(bfv) << ", " <<
at_c<2>(bfv) << endl;
using boost::tuple;
tuple<double, double, double> btt(reinterpret_cast<tuple<double,
double, double>&>(ba) ); // works!
clog << "btt: " << btt.get<0>() << ", "<< (btt).get<1>() << ", " <<
(btt).get<2>() << endl;
return 0;
}
outputs:
ba: 1, 2.1, 3.2 //original data
bfv: -9.25597e+61, -2.35344e-185, 4.87507e-270 //not expected
btt: 1, 2.1, 3.2 //works!
Let me give some context, the reason I am doing this is that I need to
interface fusion::vector of boost::quantities (all have the sizeof
double) and adimensionalize the list of quantities into an array of
doubles and vise-versa
In reality, the final goal is to get this to work:
boost::array<double, 3> ba={{1.0, 2.1, 3.2}};
vector<quantity<si::length>, quantity<si::time> , quantity<si::mass> >
bfv( reinterpret_cast<vector<quantity<si::length>,
quantity<si::time> , quantity<si::mass> >&>(ba) );
(or other generic option to go from array<double, N> to
vector<quantity<T1>, quantity<T2>, ... quantity<TN> >)
but since I can't make it work with simple 'doubles' I am asking why
this could be done.
Thank you again,
Alfredo
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