[fusion] creating fusion-vectors of references
Hi list, I am new to fusion, so my problem might be really easy to solve. I have a fusion vector which looks a bit like typedef fusion::vector<std::vector<A>,std::vector<B>,std::vector<C> > Tuple; All vectors have the same size and I want from each of these std::vectors one specific element (like the 10th from every vector) and create a fusion::vector<A&,B&,C&> from these elements(my real problem is a bit more complicated, for example one of the vectors might be std::vector<bool>, so the type might be different from an actual reference) My first try was transform: //the Functor which extracts the i-th element class Get { private: std::size_t m_index; public: Get(std::size_t index):m_index(index){} template<typename Sig> struct result; template<typename U> struct result<Get(U&)>{ typedef typename U::reference type; }; template <typename U> typename result<Get(U)&>::type operator()(U& vec) const{ return vec[m_index]; } }; Tuple myTuple(...); fusion::vector<A&,B&,C&> ref = boost::fusion::transform(myTuple,Get(0)); But this doesn't work, because 1. transform only takes myTuple as constant argument. 2. it performs a value-initialization of the references in the constructor, not in the initialization list. Any solution to my problem? It should work for fusion::vector, but a solution which works for all fusion sequences is awarded with bonus points :). If there is no solution for fusion::vector, I would appreciate a solution for another container as well (but no bonus in this case!) Greetings, Oswin
"Oswin Krause" <Oswin.Krause@ruhr-uni-bochum.de> writes:
I have a fusion vector which looks a bit like
typedef fusion::vector<std::vector<A>,std::vector<B>,std::vector<C> > Tuple;
All vectors have the same size and I want from each of these std::vectors one specific element (like the 10th from every vector) and create a fusion::vector<A&,B&,C&> from these elements(my real problem is a bit more complicated, for example one of the vectors might be std::vector<bool>, so the type might be different from an actual reference) [...] Any solution to my problem? It should work for fusion::vector, but a solution which works for all fusion sequences is awarded with bonus points :). If there is no solution for fusion::vector, I would appreciate a solution for another container as well (but no bonus in this case!)
This problem can easily be solved using Boost.MPL. (Including <boost/fusion/mpl.hpp> will make all Fusion container types valid MPL sequences.) Christopher
Hi, thanks for your answer! Unfortunately my problem is not calculating the correct types (like fusion::vector<A&,B&,C&> from the Tuple). As my example suggests, I have a problem of creating the actual object after deducing the correct types. One of the errors I get is for example( after using const_cast to get the correct reference, see problem 1.): boost/include/boost/fusion/container/vector/detail/preprocessed/vector10.hpp:101:9: error: value-initialization of ‘int& boost::fusion::vector_data2<int&, double&> >::m0’, which has reference type in the sequence constructor of vector_data2. So what happens is that fusion::vector assumes, that m0 is default constructible (references are not) and that then every object can be set to the correct value with operator= (which is not possible for references). Greetings, Oswin On 2012-08-03 16:26, Christopher Schmidt wrote:
"Oswin Krause" <Oswin.Krause@ruhr-uni-bochum.de> writes:
I have a fusion vector which looks a bit like
typedef fusion::vector<std::vector<A>,std::vector<B>,std::vector<C>
Tuple;
All vectors have the same size and I want from each of these std::vectors one specific element (like the 10th from every vector) and create a fusion::vector<A&,B&,C&> from these elements(my real problem is a bit more complicated, for example one of the vectors might be std::vector<bool>, so the type might be different from an actual reference) [...] Any solution to my problem? It should work for fusion::vector, but a solution which works for all fusion sequences is awarded with bonus points :). If there is no solution for fusion::vector, I would appreciate a solution for another container as well (but no bonus in this case!)
This problem can easily be solved using Boost.MPL. (Including <boost/fusion/mpl.hpp> will make all Fusion container types valid MPL sequences.)
Christopher _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
"Oswin Krause" <Oswin.Krause@ruhr-uni-bochum.de> writes:
Unfortunately my problem is not calculating the correct types (like fusion::vector<A&,B&,C&> from the Tuple). As my example suggests, I have a problem of creating the actual object after deducing the correct types.
One of the errors I get is for example( after using const_cast to get the correct reference, see problem 1.): boost/include/boost/fusion/container/vector/detail/preprocessed/vector10.hpp:101:9: error: value-initialization of ‘int& boost::fusion::vector_data2<int&, double&>
::m0’, which has reference type
in the sequence constructor of vector_data2. So what happens is that fusion::vector assumes, that m0 is default constructible (references are not) and that then every object can be set to the correct value with operator= (which is not possible for references).
Initialise from a fusion sequence of objects that are convertible to your target references. Christopher
Thanks, The problem was successfully located between keyboard and chair. *Awards two points bonus for immediately knowing, that there can't be a Problem in fusion* :) Have a nice weekend, Oswin Krause On 2012-08-03 16:53, Christopher Schmidt wrote:
"Oswin Krause" <Oswin.Krause@ruhr-uni-bochum.de> writes:
Unfortunately my problem is not calculating the correct types (like fusion::vector<A&,B&,C&> from the Tuple). As my example suggests, I have a problem of creating the actual object after deducing the correct types.
One of the errors I get is for example( after using const_cast to get the correct reference, see problem 1.):
boost/include/boost/fusion/container/vector/detail/preprocessed/vector10.hpp:101:9: error: value-initialization of ‘int& boost::fusion::vector_data2<int&, double&>
::m0’, which has reference type
in the sequence constructor of vector_data2. So what happens is that fusion::vector assumes, that m0 is default constructible (references are not) and that then every object can be set to the correct value with operator= (which is not possible for references).
Initialise from a fusion sequence of objects that are convertible to your target references.
Christopher _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Christopher Schmidt
-
Oswin Krause