What is the difference between passing span <T> and std :: array as arguments?
In his C ++ Fundamentals , Bjarne Stroustrup recommends using span when passing arrays by reference. Why not just pass the std :: array object?
+6
1 answer
Passing
std::array
by value will copy them. The point ofgsl::span
is that the function that takes them refers to an existing dataset.gsl::span
is capable of accepting arrays with specific runtime sizes.std::array
is fixed at compile time.gsl::span
doesn't care which type belongs to the array; it's just a pointer + size. Thus, thespan
interface can be passed fromstd::vector
,QVector
and many other types. Thestd::array
based interface requires you to use this specific container.
+10