From a0f9fd2a073226056ab5f1b17a8212f5e103deba Mon Sep 17 00:00:00 2001 From: "Jordan Maples [MSFT]" <49793787+JordanMaples@users.noreply.github.com> Date: Fri, 28 Aug 2020 10:14:28 -0700 Subject: [PATCH] A quick explanation of the differences between gsl::span and std::span --- gsl::span-and-std::span.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 gsl::span-and-std::span.md diff --git a/gsl::span-and-std::span.md b/gsl::span-and-std::span.md new file mode 100644 index 0000000..a4d0b5d --- /dev/null +++ b/gsl::span-and-std::span.md @@ -0,0 +1,20 @@ +# Span +## What is span +A span is a view over memory. It does not own the memory and is only a way to access contiguous sequences of objects. + +The `gsl::span` is based on the standardized version of `std::span` which was added to C++20. Originally, the plan was to deprecate `gsl::span` when `std::span` finished standardization, however that plan changed when the runtime bounds checking was removed from `std::span`'s design. + +## Differences between `gsl::span` and `std::span` +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. + +## Which version of span should I use? +### Use gsl::span if: +- you want to guarantee bounds safety in your project. + - All data accessing operations use bounds checking to ensure you are only accessing valid memory. +- your project uses C++14 or C++17. + - `std::span` as it was not introduced into the STL until C++20. +### Use `std::span` if: +- your project is C++20 and you need the performance offered by `std::span`. + +# span_iterator +Like `gsl::span`, `gsl::span`'s iterators also differ from `std::span`'s iterator in that all access operations are bounds checked. \ No newline at end of file