Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between std::span and gsl::span?

In the 2010's, when std::span was not yet standardized, the C++ Core Guidelines project got started. It quickly developed a small library of constructs which authors found they need to recommend in order to be able to abide by the guidelines - the guidelines support library (GSL) (link is to Microsoft's implementation). Among other things, the GSL includes a span class: gsl::span<T>. Eventually, in 2020, std::span was officially standardized.

Recently, as I mentioned std::span to someone, they told me:

std::span is a trap, gsl::span should be used instead

what could they have meant? Isn't std::span supposed to be basically the same thing as gsl::span? I mean, after all, it's basically just a structure with a pointer and a size_t, and some convenience methods, isn't it?

like image 668
einpoklum Avatar asked Oct 26 '25 13:10

einpoklum


1 Answers

From the GSL span class' documentation page (emphasis mine):

The only difference between gsl::span and std::span is that gsl::span strictly enforces runtime bounds checking. Any violations of the bounds check results in termination of the program.

and the same goes for the two span classes' iterators.

like image 196
einpoklum Avatar answered Oct 29 '25 05:10

einpoklum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!