boost: Using matrix_row in place of a vector

582 views Asked by At

I have a function taking a vector and modifying it. How can I pass a matrix_row instance to this function? I don't want to do any copy operations

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/numeric/ublas/vector.hpp>
using namespace boost::numeric::ublas;

void useMatrixRow(matrix_row<matrix<double> >& mRow) {
//  ...
}
void useConstVector(const vector<double>& v) {
//  ...
}
void useVector(vector<double>& v) {
//  ...
}
void useMatrix(matrix<double>& m) {
    matrix_row<matrix<double> > mRow(m, 0);
    useMatrixRow(mRow); // works
    useConstVector(mRow); // works
    // useVector(mRow); // doesn't work
}

When uncommenting the useVector(mRow) expression I get:

error: invalid initialization of reference of type 'boost::numeric::ublas::vector<double>&' from expression of type 'boost::numeric::ublas::matrix_row<boost::numeric::ublas::matrix<double> >'
src/PythonWrapper.cpp:60:6: error: in passing argument 1 of 'void useVector(boost::numeric::ublas::vector<double>&)'
1

There are 1 answers

1
ales_t On BEST ANSWER

You could make useVector a template function:

template<class T>
void useVector(T &v) {
...
}

Alternatively (if possible), pass iterators instead of the whole container:

template<class IterT>
void useVector(IterT begin, IterT end) {
...
}