We have a large code base mainly written in C
. We do a lot of numerical linear algebra.
In C
we use double*
, with length 3 to model a 3d vector.
In C++
we use std:array<double,3>
to model a 3d vector.
We have a small template library to overload a lot of operators, such as
template <class T, size_t N>
std::array<T, N> operator+(const std::array<T, N> &array1, const std::array<T, N> &array2) {
std::array<T, N> a;
for (size_t i = 0; i < N; i++) {
a[i] = array1[i] + array2[i];
}
return a;
}
The problem comes, when we need to go from C
to C++
(the other way around is no problem, since we can use the .data() method to access the aligned memory). I do not like to make a copy of each c-like 3d vector, because this would be numerically expensive.
Can I treat a pod array as a std::array or is there a way of initializing an std::array in a way that no new memory is allocated, but the memory from the pod array is used?
I am thinking about using gsl::span
from the guideline support library, but I wonder if there is a better solution.