std::span works
great on std::vectors, std::arrays or c pointers. However, often the contiguous memory has a different type associated to it such as for example a vector of arrays. I'm trying to use a std::span
on vectors of arrays, arrays of arrays, or more generally containers of contiguous data.
For example, if I have a function:
void func(std::span<double> vec){
}
and a vector of array:
std::vector<std::array<double,3>> vec;
I would like to be able to call it like that:
func(vec);
I would like to avoid the std::span<std::array<T,N>>
since this is all one block of memory, and I would like to accept std::vector<T>
, std::array<T>
, and std::vector<std::array<T>>
as well. It is possible to do something like that with raw pointer, but then the type is lost and it decays, so I am trying to avoid that.
I could do func(std::span(&vec[0][0], vec.size()*3))
, but I am wondering if there is something better.
template <typename Container2D> func(Container2D cont)
and if you want to you can use concepts to enforce the interface you want.const
reference;std::span
is cheap to pass, actual containers would involve copies if not received by reference.