I know this might overlap with the question What is a “span” and when should I use one?, but I think the answer to this specific part of the question is pretty confusing. On one hand, there are quotes like this:
Don't use it if you have a standard library container (or a Boost container etc.) which you know is the right fit for your code. It's not intended to supplant any of them.
But in the same answer, this statement occurs:
is the reasonable alternative to passing const vector& to functions when you expect your data to be contiguous in memory. No more getting scolded by high-and-mighty C++ gurus!
So what part am I not getting here? When would I do this:
void foo(const std::vector<int>& vec) {}
And when this?
void foo(std::span<int> sp) {}
Also, would this
void foo(const std::span<int> sp) {}
make any sense? I figured that it shouldn't, because a std::span
is just a struct
, containing a pointer and the length. But if it doesn't prevent you from changing the values of the std::vector
you passed as an argument, how can it replace a const std::vector<T>&
?
store()
demonstrates that astd::vector
should be constructible from any container with elements of the proper type. That it isn't is bad. Still, the interface should not be burdened by that insufficiency of the target, especially as there is a good-enough workaround. 2.foo()
demonstrates that not usingstd::span
where appropriate is contagious. It has to be fixed first at the leaves. – Seismography