I'm trying to write a C++ function which is called from Fortran. Fortran is therefore passing all arguments (vectors) by reference to the C++ function. In order to omit copying of the data I want to create a data structure which interprets the Fortran references as ublas::vectors. So far I managed to get this working using standard arrays with doubles (see variable declaration in struct):
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
template<unsigned int sizeVector>
struct DataContainer
{
typedef boost::numeric::ublas::bounded_vector<double,sizeVector> Vector;
// Variable declaration
double (&var1)[sizeVector];
double (&var2)[sizeVector];
// Constructor
DataContainer(double *var1_, double *var2_): var1(*static_cast<double(*)[sizeVector]>(static_cast<void*>(var1_))),
var2(*static_cast<double(*)[sizeVector]>(static_cast<void*>(var2_))) {
}
}
// var1 and var2 are vectors (arrays) by reference as input
extern "C" void someFunction_(double *var1, double *var2)
{
// Example --> DataContainer with vectors of sizeVector=6
DataContainer<6> mydata(var1, var2);
}
Unfortunately I don't know how to cast the pointer to ublas:vector (see typedef in struct) instead of a array of doubles. Something like this does not work:
boost::numeric::ublas::bounded_vector<double,2> &var1 = *reinterpret_cast<boost::numeric::ublas::bounded_vector<double,2>*> (var1_);