0

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 Answer 1

1

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) {
...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I was afraid there is no "cleaner" solution than using a template function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.