1

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_);    
don
  • 11
  • 2
  • You don't cast this at all, because these are two different types. If `ublas::bounded_vector` had a constructor taking a `double*` and a length, you could initialize it, e.g. `boost::numeric::ublas::bounded_vector var1(var1_, var1_len)`. But I haven't seen such a constructor, so the only choice might be via a temporary `vector`. – Olaf Dietsche Sep 13 '17 at 13:02

0 Answers0