1

我正在尝试编写一个从 Fortran 调用的 C++ 函数。因此,Fortran 通过引用 C++ 函数来传递所有参数(向量)。为了省略数据的复制,我想创建一个将 Fortran 引用解释为 ublas::vectors 的数据结构。到目前为止,我设法使用带双精度的标准数组来实现这一点(请参阅结构中的变量声明):

#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);
}

不幸的是,我不知道如何将指针转换为 ublas:vector(请参阅 struct 中的 typedef)而不是双精度数组。像这样的东西不起作用:

boost::numeric::ublas::bounded_vector<double,2> &var1 = *reinterpret_cast<boost::numeric::ublas::bounded_vector<double,2>*> (var1_);    
4

0 回答 0