3

I'm trying to implement a "data frame" type class in C++ (like in S+/R), something along the lines of:

template<typename T1, typename T2, typename T3>
class data_frame;

My goal here is to have the data stored in memory as "column vectors" (rather than as a simple vector of tuples), for reasons which are not really relevant to the question.

(assume I have no variadic templates, and that I'm not worried right now about the fixed number of parameters -- I can work around this later).

Using MPL and fusion, I've so far been able to create a fusion sequence of std:vectors:

template <typename T> 
struct make_vector 
{ 
    typedef std::vector<T> type; 
}; 
typedef boost::mpl::vector<T1, T2, T3> data_types;
typedef boost::mpl::transform1<data_types, make_vector<_1> >  vector_types;
typedef boost::fusion::result_of::as_vector<vector_types>::type vectors;

I've also managed to define a tuple_type, which is the specialization of a boost::tuple with the same signature as the data_frame.

typedef details_not_shown tuple_type;

Now, I would like to define a member function that allows me to add a tuple_type to the data_frame. Here's what I have in mind:

vectors the_vectors;

struct append
{
    typedef void result_type;

    template<typename U>
    void operator()(const U & d, std::vector<U> & v) const
    {
        v.push_back(d);
    }
}

void push_back(tuple_type & t)
{
    fusion::for_each(fusion::zip(t,the_vectors), fusion::make_fused_function_object(append())) ;
}

When I compile this (on VS2010 C++), I get the following error:

error C2664: 'void some_class<T0,T1,T2>::append::operator ()<const T>(const U &,std::vector<_Ty> &) const'
: cannot convert parameter 2 from 'const std::vector<_Ty>' to 'std::vector<_Ty> &'

Apparently the zip is making a copy of the std:vector elements of the_vectors rather than passing a reference, so the push_back fails.

Does anyone know how I can get the fusion zip to pass a reference rather than a copy?

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
David H
  • 1,461
  • 2
  • 17
  • 37

0 Answers0