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
source share
1 answer
  • Passing std::array by value will copy them. The point of gsl::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, the span interface can be passed from std::vector , QVector and many other types. The std::array based interface requires you to use this specific container.

+10
source

Source: https://habr.com/ru/post/1013843/


All Articles