On Oct 6, 5:33 pm, Joel de Guzman 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 to vector:
I found the feature of the library that allows conversion from
array to vector. Just by including
#include
#include
boost::array ba={{1.0, 2.1, 3.2}};
vector bfv1(ba); //works.
I still can the other way around.
boost::array ba0(bfv1); //doesn't work
ideas?
great, I also managed to solve the original problem on how to convert
from
boost::array to
fusion::vector
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::vectorsi::length,
quantitysi::time, quantitysi::mass > vector_of_q;
vector_of_q fvq(
transform(
ba, // boost::array // 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{
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 to
array 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 to
array. ideas?
Thank you for making fusion, it is an incredible library once you
realize what it is for.
Alfredo