Skip to content

Commit 2d343b0

Browse files
Document differences between hardened std::span and gsl::span (#1206)
* Initial plan for issue * Document differences between hardened std::span and gsl::span Co-authored-by: carsonRadtke <[email protected]> * Simplify documentation for span differences Co-authored-by: carsonRadtke <[email protected]> * Enhance span documentation with comparison table and links Co-authored-by: carsonRadtke <[email protected]> * Fix library hardening documentation links Co-authored-by: carsonRadtke <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: carsonRadtke <[email protected]>
1 parent c219709 commit 2d343b0

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

docs/headers.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -412,22 +412,36 @@ The `gsl::span` is based on the standardized version of `std::span` which was ad
412412
deprecate `gsl::span` when `std::span` finished standardization, however that plan changed when the runtime bounds checking
413413
was removed from `std::span`'s design.
414414
415-
The only difference between `gsl::span` and `std::span` is that `gsl::span` strictly enforces runtime bounds checking.
416-
Any violations of the bounds check results in termination of the program.
417-
Like `gsl::span`, `gsl::span`'s iterators also differ from `std::span`'s iterator in that all access operations are bounds checked.
415+
The key differences between `gsl::span` and `std::span` are:
416+
- `gsl::span` strictly enforces runtime bounds checking for all access operations
417+
- Any violations of the bounds check results in termination of the program
418+
- `gsl::span`'s iterators also perform bounds checking, unlike `std::span`'s iterators
418419
419420
#### Which version of span should I use?
420421
421-
##### Use `gsl::span` if
422-
423-
- you want to guarantee bounds safety in your project.
424-
- All data accessing operations use bounds checking to ensure you are only accessing valid memory.
425-
- your project uses C++14 or C++17.
426-
- `std::span` is not available as it was not introduced into the STL until C++20.
427-
428-
##### Use `std::span` if
429-
430-
- your project is C++20 and you need the performance offered by `std::span`.
422+
The following table compares the different span implementations to help you choose which one is best for your project:
423+
424+
| Feature/Version | `std::span` (C++20/23) | Hardened `std::span` (C++26) | `gsl::span` |
425+
|-----------------|------------------------|------------------------------|-------------|
426+
| **C++ Standard** | Requires C++20 or later | Requires C++26 or backported implementation | Works with C++14 or later |
427+
| **Element Access** | No bounds checking | Bounds checking | Bounds checking |
428+
| **Iterator Safety** | No bounds checking | Implementation-defined, may depend on vendor | Full bounds checking |
429+
| **Error Behavior** | Undefined behavior on invalid access | Implementation-defined, may be configurable | Always calls [`std::terminate()`](https://en.cppreference.com/w/cpp/error/terminate) via [gsl::details::terminate()](https://github.com/microsoft/GSL/blob/main/include/gsl/assert#L111-L118) |
430+
| **Performance** | Fastest (no checking) | Varies by implementation and configuration | May have performance impact from bounds checking |
431+
432+
**Recommendations:**
433+
434+
- **C++14 & C++17 projects**: Use `gsl::span` as `std::span` is not available.
435+
- **C++20 & C++23 projects**:
436+
- Use `gsl::span` if safety is your priority.
437+
- Use `std::span` if performance is critical and you're confident in your index calculations.
438+
- **C++26 projects**:
439+
- Use `gsl::span` if you need guaranteed iterator safety across all platforms.
440+
- Use hardened `std::span` if you want standard library compliance and acceptable safety.
441+
442+
**Implementation notes for hardened `std::span` in C++26:**
443+
- For MSVC: See [Microsoft STL Hardening](https://github.com/microsoft/STL/wiki/STL-Hardening)
444+
- For Clang/LLVM: See [libc++ Hardening](https://libcxx.llvm.org/Hardening.html)
431445
432446
#### Types
433447

0 commit comments

Comments
 (0)