Compare commits

...

22 Commits
v4.1.0 ... main

Author SHA1 Message Date
Werner Henze
3325bbd33d
Fix version info (#1202)
Some checks failed
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled
Just tagging as 4.2.0 is not enough, the version number must be incremented for cmake and in the readme. Compare https://github.com/microsoft/GSL/pull/1163.

Co-authored-by: Werner Henze <w.henze@avm.de>
2025-03-27 11:00:24 -05:00
Werner Henze
2828399820
replace BYTE_TYPE with a solution based on namespaces (#1201)
Some checks failed
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled
- A macro with the very generic name `BYTE_TYPE` is likely to collide with existing code, so get rid of the macro.
- The new solution is to provide a non-deprecated `byte` in the namespace `gsl::impl`.
  - Users of GSL should use `gsl::byte`, which is still deprecated when mapped to a `std::std::byte`.
  - GSL types and functions need to use `gsl::impl::byte` so they do not trigger the deprecation warning.
- The `gsl::impl::byte` return type in an exported function is not nice, it might mislead users to use that type in their own declarations. But the `BYTE_TYPE` solution is not better in this respect.

Co-authored-by: Werner Henze <w.henze@avm.de>
2025-02-28 08:53:25 -06:00
Werner Henze
c5fbb81ad4
documentation (#1200)
- reintroduce documentation internal links
- remove `gsl::` prefix in table so it is the same as in the other tables
- fix documentation for span::element_type

Co-authored-by: Werner Henze <w.henze@avm.de>
2025-02-28 08:49:53 -06:00
Carson Radtke
7fabaa499d
deprecate features adopted into C++ (#1198)
Some checks failed
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled
* deprecated features adopted into C++

1) Mark the following GSL features as deprecated:
 - gsl::unique_ptr (always)
 - gsl::shared_ptr (always)
 - gsl::byte (since c++17)
 - gsl::joining_thread (never implemented)
2) Refactor existing deprecations to use the new GSL_DEPRECATED(msg) macro.
3) Create a section in the README for deprecated features in the
   standard.

* do not deprecate gsl::to_integer because we never claim to implement it.

* do not use gsl::byte if it is deprecated
2025-02-14 11:24:51 -06:00
Carson Radtke
4742bc192a
use relative path in # include (#1194)
Some checks failed
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled
Reverts commit that changes #include "assert" -> #include "gsl/assert".
This change is necessary in order to comply with CppCoreGuideline's
SF.12. Now we do #include "./assert".
2025-02-12 09:16:45 -06:00
Carson Radtke
ec729d63a7
introduce gsl::not_null<T>::element_type (#1196)
* introduce gsl::not_null<T>::element_type

* use std::is_same instead of is_same_v

* fix: cannot put a non-pointer in a gsl::not_null
2025-02-12 09:16:05 -06:00
Carson Radtke
7f4fc9388b
Add more gsl::span tests [copilot] (#1189)
* Add more gsl::span tests [copilot]

This PR adds comprehensive unit tests for `gsl::span` to ensure its correctness and consistency. The following tests have been added:

1. **Empty Span Tests**:
   - Tests to verify the behavior of an empty `gsl::span` and `gsl::span<const int>`.

2. **Conversion Tests**:
   - Tests to check the conversion between different types of `gsl::span`.

3. **Comparison Operator Tests**:
   - Tests to verify the comparison operators for `gsl::span`.

4. **Deduction Guide Tests**:
   - Tests to compare the behavior of `gsl::span` and `std::span` deduction guides for various types of arrays and containers.

These tests help ensure that `gsl::span` behaves correctly in various scenarios and is consistent with `std::span`.

This PR was created with the help of GitHub Copilot.

**Changes**:
- Added tests for empty span.
- Added tests for conversions.
- Added tests for comparison operators.
- Added tests for deduction guides.

**Testing**:
- All new tests have been added to the existing test suite.
- Run the test suite using `ctest` to ensure all tests pass.This PR adds comprehensive unit tests for `gsl::span` to ensure its correctness and consistency. The following tests have been added:

1. **Empty Span Tests**:
   - Tests to verify the behavior of an empty `gsl::span` and `gsl::span<const int>`.

2. **Conversion Tests**:
   - Tests to check the conversion between different types of `gsl::span`.

3. **Comparison Operator Tests**:
   - Tests to verify the comparison operators for `gsl::span`.

4. **Deduction Guide Tests**:
   - Tests to compare the behavior of `gsl::span` and `std::span` deduction guides for various types of arrays and containers.

These tests help ensure that `gsl::span` behaves correctly in various scenarios and is consistent with `std::span`.

This PR was created with the help of GitHub Copilot.

**Changes**:
- Added tests for empty span.
- Added tests for conversions.
- Added tests for comparison operators.
- Added tests for deduction guides.

**Testing**:
- All new tests have been added to the existing test suite.
- Run the test suite using `ctest` to ensure all tests pass.

* fix tests for pre-C++17
2025-02-12 09:15:43 -06:00
Tiago
355982daf6
Delete gsl/string_span (#1185)
Some checks failed
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled
This has been deprecated for 2+ years.
2025-01-07 08:58:59 -06:00
Werner Henze
50aaf2efbb
remove documentation for deprecated make_span overloads (#1183)
Co-authored-by: Werner Henze <werner.henze+gitcommits@posteo.de>
2025-01-07 08:50:49 -06:00
Tiago
7b6b8013b1
Add "gsl" to #includes (#1184)
Office is seeing build breaks due to `#include "span"` including
C++20 span instead of gsl/span. Most likely we want all headers
includes qualified with "gsl/" to avoid similar issues.
2025-01-07 08:42:38 -06:00
Werner Henze
1cdb8d295e
More checks for non-compilable code, plus fix for span (#1180)
Some checks failed
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled
2025-01-04 11:50:31 -06:00
Carson Radtke
c832885f15
React to ubuntu-latest changing to 24.04 (#1181)
Some checks failed
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled
* React to ubuntu-latest changing to 24.04
Reacting to https://github.com/actions/runner-images/issues/10636
* update clang to 16,17,18
* update gcc to 12,13,14
2025-01-03 10:23:50 -06:00
Werner Henze
16a60199df
strict_not_null for unique_ptr (#1179)
Some checks failed
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled
- `strict_not_null<std::unique_ptr<int>>{ std::make_unique<int>()}` failed to compile
  - `strict_not_null` ctor needs to move the passed `unique_ptr`, not copy
  - Copied `not_null` `TestNotNullConstructors` for `strict_not_null`
- The `noexcept` specifiers on the `strict_not_null` and `not_null` constructors were out of sync.
- Added unit test for `not_null<unique_ptr<T>>` and for `strict_not_null<unique_ptr<T>>`
- Added unit test for `gsl::swap` for two `strict_not_null`
- Added unit test for `gsl::swap` for `not_null` and `strict_not_null`

Co-authored-by: Werner Henze <w.henze@avm.de>
2024-12-26 11:00:36 -06:00
Werner Henze
fcd55ee924
Better use of std::enable_if (#1177)
Some checks failed
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled
* this commits adds tests that should fail, but don't

* Better use of std::enable_if

Replace the occurances of `class = std::enable_if_t<Cond>` and `typename = std::enable_if_t<Cond>` that have been identified in the previous commit with `std::enable_if_t<Cond, bool> = true`.

This commit is inspired by #1174, which changed one occurance in the owner header. This commit is aimed to fix all remaining occurances.

* fix failing checks

- core.cxx_gsl aktualisiert auf [](https://gitlab.avm.de/fos/repos/core.cxx_gsl/-/commit/)
- plc.access_lib aktualisiert auf [](https://gitlab.avm.de/fos/repos/plc.access_lib/-/commit/)
- plc.common aktualisiert auf [](https://gitlab.avm.de/fos/repos/plc.common/-/commit/)
- plc.daemon aktualisiert auf [](https://gitlab.avm.de/fos/repos/plc.daemon/-/commit/)
-

Test Plan:
-

---------

Co-authored-by: Werner Henze <werner.henze+gitcommits@posteo.de>
Co-authored-by: Werner Henze <w.henze@avm.de>
2024-12-23 10:40:22 -06:00
Werner Henze
b8ac820fe1
Force unit tests for byte to use GSL implementation (#1176)
Some checks failed
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled
When setting `GSL_USE_STD_BYTE` to zero, then the unit test does not compile because `byte` is ambiguous (could be `std::byte` or `gsl::byte`). So `gsl::` prefix is needed for `byte`.
It looks like the unit tests never ran on a platform where `gsl::byte` is not based on `std::byte`.
It does not make much sense to test `std::byte` for compliance, so make the unit tests based on the GSL implementation of `gsl::byte` (`#define GSL_USE_STD_BYTE 0`).

Co-authored-by: Werner Henze <werner.henze+gitcommits@posteo.de>
2024-12-18 10:16:36 -06:00
Carson Radtke
272463043e
introduce gsl::swap for swapping gsl::not_null (#1160)
Some checks failed
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled
fixes: https://github.com/microsoft/GSL/issues/1129

* create gsl::swap<T>(T&, T&) which wraps std::swap
* specialize gsl::swap<T>(gsl::not_null<T>&, gsl::not_null<T>&)
* add tests
2024-12-13 15:16:53 -06:00
Carson Radtke
8a0e3d8a9b
fix: direct-init const ref instead of list-init (#1175)
fixes: https://github.com/microsoft/GSL/issues/1162

gcc has a bug that generates a call to the copy constructor when list
initializing a const reference. This PR offers a workaround which is to
direct initialize the value.

see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117900
2024-12-13 10:22:16 -06:00
Alberto La Rocca
aed09c41b6
Fix SFINAE on gsl::owner. (#1174)
`std::enable_if_t` must not be used as a default template argument, otherwise the instantiator will be able to override it freely with something that doesn't fail substitution. Instead, `std::enable_if_t` itself must be the type of the template argument.

More information in the examples here: https://en.cppreference.com/w/cpp/types/enable_if
2024-12-13 09:56:28 -06:00
Carson Radtke
4b190d2e2a
Create issue templates (#1173)
Some checks failed
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled
2024-12-02 14:34:38 -06:00
Carson Radtke
ddae9d72b6
fix stale badge links in README (#1170)
Some checks failed
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled
* update pipeline badge links
* add link to compilers pipeline
* add badge to vcpkg version
2024-11-25 13:06:02 -06:00
Carson Radtke
74d2bb79d4
fix various pipeline failures (#1172)
* fix failing pipeline tests
* upgrade to googletest v1.14.0 so it works with newer cmake versions
* fix android pipeline to permit new cmake versions (short-term fix)
2024-11-25 12:58:45 -06:00
Carson Radtke
f8ec309118
improve performance of span_iterator w/ clang (#1166)
Some checks failed
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled
* improve performance of span_iterator w/ clang

Issue: #1165

Before this PR, the range-for loop was ~3300x slower. After this PR, it
is ~1.005x slower

The clang optimizer is very good at optimizing `current != end`, so
we changed to this idiom. This moves the Expects assertion into the
constructor instead of on the hot-path which is called whenever either
operator++ or operator* is called.

Note: The codegen for the assertion is still a missed optimization,
but less worrisome as it only happens once per iterator.

Note: benchmarks on M1 Macbook Pro w/ Apple Clang 16.0.0
2024-11-12 15:41:21 -06:00
26 changed files with 1061 additions and 487 deletions

29
.github/ISSUE_TEMPLATE/bug_report.md vendored Normal file
View File

@ -0,0 +1,29 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: 'Status: Open, Type: Bug'
assignees: ''
---
**Describe the bug**
A clear and concise description of what the bug is.
**To Reproduce**
```c++
#include <gsl>
// your repro here: ...
```
**Expected behavior**
A clear and concise description of what you expected to happen.
**Spec (please complete the following information):**
- OS: [e.g. Windows]
- Compiler: [e.g. MSVC]
- C++ Version: [e.g. C++20]
**Additional context**
Add any other context about the problem here.

View File

@ -38,7 +38,7 @@ jobs:
echo "Emulator starting in background" echo "Emulator starting in background"
- name: Configure - name: Configure
run: cmake -Werror=dev -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_LATEST_HOME/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=16 -DANDROID_ABI=x86_64 -DCMAKE_BUILD_TYPE=Debug .. run: cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_LATEST_HOME/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=16 -DANDROID_ABI=x86_64 -DCMAKE_BUILD_TYPE=Debug ..
- name: Build - name: Build
run: cmake --build . --parallel run: cmake --build . --parallel

View File

@ -17,11 +17,16 @@ jobs:
gcc: gcc:
strategy: strategy:
matrix: matrix:
gcc_version: [ 10, 11, 12 ] gcc_version: [ 12, 13, 14 ]
build_type: [ Debug, Release ] build_type: [ Debug, Release ]
cxx_version: [ 14, 17, 20, 23 ] cxx_version: [ 14, 17, 20, 23 ]
exclude: exclude:
- gcc_version: 10 # https://github.com/google/googletest/issues/4232
# Looks like GoogleTest is not interested in making version 1.14
# work with gcc-12.
- gcc_version: 12
cxx_version: 20
- gcc_version: 12
cxx_version: 23 cxx_version: 23
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
@ -37,9 +42,15 @@ jobs:
clang: clang:
strategy: strategy:
matrix: matrix:
clang_version: [ 13, 14, 15 ] clang_version: [ 16, 17, 18 ]
build_type: [ Debug, Release ] build_type: [ Debug, Release ]
cxx_version: [ 14, 17, 20, 23 ] cxx_version: [ 14, 17, 20, 23 ]
exclude:
# https://github.com/llvm/llvm-project/issues/93734
# Looks like clang fixed this issue in clang-18, but won't backport
# the fix.
- clang_version: 17
cxx_version: 23
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@ -54,7 +65,7 @@ jobs:
xcode: xcode:
strategy: strategy:
matrix: matrix:
xcode_version: [ '14.3.1', '15.4' ] xcode_version: [ '15.4' ]
build_type: [ Debug, Release ] build_type: [ Debug, Release ]
cxx_version: [ 14, 17, 20, 23 ] cxx_version: [ 14, 17, 20, 23 ]
runs-on: macos-latest runs-on: macos-latest

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.14...3.16) cmake_minimum_required(VERSION 3.14...3.16)
project(GSL VERSION 4.1.0 LANGUAGES CXX) project(GSL VERSION 4.2.0 LANGUAGES CXX)
add_library(GSL INTERFACE) add_library(GSL INTERFACE)
add_library(Microsoft.GSL::GSL ALIAS GSL) add_library(Microsoft.GSL::GSL ALIAS GSL)

View File

@ -1,5 +1,6 @@
# GSL: Guidelines Support Library # GSL: Guidelines Support Library
[![Build Status](https://dev.azure.com/cppstat/GSL/_apis/build/status/microsoft.GSL?branchName=main)](https://dev.azure.com/cppstat/GSL/_build/latest?definitionId=1&branchName=main) [![CI](https://github.com/Microsoft/GSL/actions/workflows/compilers.yml/badge.svg)](https://github.com/microsoft/GSL/actions/workflows/compilers.yml?query=branch%3Amain)
[![vcpkg](https://img.shields.io/vcpkg/v/ms-gsl)](https://vcpkg.io/en/package/ms-gsl)
The Guidelines Support Library (GSL) contains functions and types that are suggested for use by the The Guidelines Support Library (GSL) contains functions and types that are suggested for use by the
[C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines) maintained by the [Standard C++ Foundation](https://isocpp.org). [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines) maintained by the [Standard C++ Foundation](https://isocpp.org).
@ -39,8 +40,6 @@ span_p | &#x26
[u32zstring](docs/headers.md#user-content-H-zstring) | &#x2611; | An alias to `basic_zstring` with dynamic extent and a char type of `char32_t` [u32zstring](docs/headers.md#user-content-H-zstring) | &#x2611; | An alias to `basic_zstring` with dynamic extent and a char type of `char32_t`
[cu32zstring](docs/headers.md#user-content-H-zstring) | &#x2611; | An alias to `basic_zstring` with dynamic extent and a char type of `const char32_t` [cu32zstring](docs/headers.md#user-content-H-zstring) | &#x2611; | An alias to `basic_zstring` with dynamic extent and a char type of `const char32_t`
[**2. Owners**][cg-owners] | | [**2. Owners**][cg-owners] | |
[unique_ptr](docs/headers.md#user-content-H-pointers-unique_ptr) | &#x2611; | An alias to `std::unique_ptr`
[shared_ptr](docs/headers.md#user-content-H-pointers-shared_ptr) | &#x2611; | An alias to `std::shared_ptr`
stack_array | &#x2610; | A stack-allocated array stack_array | &#x2610; | A stack-allocated array
dyn_array | &#x2610; | A heap-allocated array dyn_array | &#x2610; | A heap-allocated array
[**3. Assertions**][cg-assertions] | | [**3. Assertions**][cg-assertions] | |
@ -48,13 +47,11 @@ dyn_array | &#x26
[Ensures](docs/headers.md#user-content-H-assert-ensures) | &#x2611; | A postcondition assertion; on failure it terminates [Ensures](docs/headers.md#user-content-H-assert-ensures) | &#x2611; | A postcondition assertion; on failure it terminates
[**4. Utilities**][cg-utilities] | | [**4. Utilities**][cg-utilities] | |
move_owner | &#x2610; | A helper function that moves one `owner` to the other move_owner | &#x2610; | A helper function that moves one `owner` to the other
[byte](docs/headers.md#user-content-H-byte-byte) | &#x2611; | Either an alias to `std::byte` or a byte type
[final_action](docs/headers.md#user-content-H-util-final_action) | &#x2611; | A RAII style class that invokes a functor on its destruction [final_action](docs/headers.md#user-content-H-util-final_action) | &#x2611; | A RAII style class that invokes a functor on its destruction
[finally](docs/headers.md#user-content-H-util-finally) | &#x2611; | A helper function instantiating [final_action](docs/headers.md#user-content-H-util-final_action) [finally](docs/headers.md#user-content-H-util-finally) | &#x2611; | A helper function instantiating [final_action](docs/headers.md#user-content-H-util-final_action)
[GSL_SUPPRESS](docs/headers.md#user-content-H-assert-gsl_suppress) | &#x2611; | A macro that takes an argument and turns it into `[[gsl::suppress(x)]]` or `[[gsl::suppress("x")]]` [GSL_SUPPRESS](docs/headers.md#user-content-H-assert-gsl_suppress) | &#x2611; | A macro that takes an argument and turns it into `[[gsl::suppress(x)]]` or `[[gsl::suppress("x")]]`
[[implicit]] | &#x2610; | A "marker" to put on single-argument constructors to explicitly make them non-explicit [[implicit]] | &#x2610; | A "marker" to put on single-argument constructors to explicitly make them non-explicit
[index](docs/headers.md#user-content-H-util-index) | &#x2611; | A type to use for all container and array indexing (currently an alias for `std::ptrdiff_t`) [index](docs/headers.md#user-content-H-util-index) | &#x2611; | A type to use for all container and array indexing (currently an alias for `std::ptrdiff_t`)
joining_thread | &#x2610; | A RAII style version of `std::thread` that joins
[narrow](docs/headers.md#user-content-H-narrow-narrow) | &#x2611; | A checked version of `narrow_cast`; it can throw [narrowing_error](docs/headers.md#user-content-H-narrow-narrowing_error) [narrow](docs/headers.md#user-content-H-narrow-narrow) | &#x2611; | A checked version of `narrow_cast`; it can throw [narrowing_error](docs/headers.md#user-content-H-narrow-narrowing_error)
[narrow_cast](docs/headers.md#user-content-H-util-narrow_cast) | &#x2611; | A narrowing cast for values and a synonym for `static_cast` [narrow_cast](docs/headers.md#user-content-H-util-narrow_cast) | &#x2611; | A narrowing cast for values and a synonym for `static_cast`
[narrowing_error](docs/headers.md#user-content-H-narrow-narrowing_error) | &#x2611; | A custom exception type thrown by [narrow](docs/headers.md#user-content-H-narrow-narrow) [narrowing_error](docs/headers.md#user-content-H-narrow-narrowing_error) | &#x2611; | A custom exception type thrown by [narrow](docs/headers.md#user-content-H-narrow-narrow)
@ -76,6 +73,14 @@ cu16string_span | &#x2610; | Deprecated. An alias to `basic
u32string_span | &#x2610; | Deprecated. An alias to `basic_string_span` with a char type of `char32_t` u32string_span | &#x2610; | Deprecated. An alias to `basic_string_span` with a char type of `char32_t`
cu32string_span | &#x2610; | Deprecated. An alias to `basic_string_span` with a char type of `const char32_t` cu32string_span | &#x2610; | Deprecated. An alias to `basic_string_span` with a char type of `const char32_t`
## The following features have been adopted by WG21. They are deprecated in GSL.
Feature | Deprecated Since | Notes
------------------------------------------------------------------|------------------|------
[unique_ptr](docs/headers.md#user-content-H-pointers-unique_ptr) | C++11 | Use std::unique_ptr instead.
[shared_ptr](docs/headers.md#user-content-H-pointers-shared_ptr) | C++11 | Use std::shared_ptr instead.
[byte](docs/headers.md#user-content-H-byte-byte) | C++17 | Use std::byte instead.
joining_thread | C++20 (Note: Not yet implemented in GSL) | Use std::jthread instead.
This is based on [CppCoreGuidelines semi-specification](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#gsl-guidelines-support-library). This is based on [CppCoreGuidelines semi-specification](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#gsl-guidelines-support-library).
[cg-views]: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#gslview-views [cg-views]: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#gslview-views
@ -92,9 +97,9 @@ Below is a table showing the versions currently being tested (also see [.github/
Compiler |Toolset Versions Currently Tested Compiler |Toolset Versions Currently Tested
:------- |--: :------- |--:
GCC | 10, 11, 12 GCC | 12, 13, 14
XCode | 14.3.1, 15.4 XCode | 14.3.1, 15.4
Clang | 13, 14, 15 Clang | 16, 17, 18
Visual Studio with MSVC | VS2019, VS2022 Visual Studio with MSVC | VS2019, VS2022
Visual Studio with LLVM | VS2019, VS2022 Visual Studio with LLVM | VS2019, VS2022
@ -106,8 +111,8 @@ If you successfully port GSL to another platform, we would love to hear from you
Target | CI/CD Status Target | CI/CD Status
:------- | -----------: :------- | -----------:
iOS | ![CI_iOS](https://github.com/microsoft/GSL/workflows/CI_iOS/badge.svg) iOS | [![CI_iOS](https://github.com/microsoft/GSL/workflows/CI_iOS/badge.svg?branch=main)](https://github.com/microsoft/GSL/actions/workflows/ios.yml?query=branch%3Amain)
Android | ![CI_Android](https://github.com/microsoft/GSL/workflows/CI_Android/badge.svg) Android | [![CI_Android](https://github.com/microsoft/GSL/workflows/CI_Android/badge.svg?branch=main)](https://github.com/microsoft/GSL/actions/workflows/android.yml?query=branch%3Amain)
Note: These CI/CD steps are run with each pull request, however failures in them are non-blocking. Note: These CI/CD steps are run with each pull request, however failures in them are non-blocking.
@ -197,7 +202,7 @@ include(FetchContent)
FetchContent_Declare(GSL FetchContent_Declare(GSL
GIT_REPOSITORY "https://github.com/microsoft/GSL" GIT_REPOSITORY "https://github.com/microsoft/GSL"
GIT_TAG "v4.1.0" GIT_TAG "v4.2.0"
GIT_SHALLOW ON GIT_SHALLOW ON
) )

View File

@ -91,16 +91,16 @@ See [SL.str.5: Use `std::byte` to refer to byte values that do not necessarily r
### Non-member functions ### Non-member functions
```cpp ```cpp
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>> template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
constexpr byte& operator<<=(byte& b, IntegerType shift) noexcept; constexpr byte& operator<<=(byte& b, IntegerType shift) noexcept;
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>> template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
constexpr byte operator<<(byte b, IntegerType shift) noexcept; constexpr byte operator<<(byte b, IntegerType shift) noexcept;
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>> template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
constexpr byte& operator>>=(byte& b, IntegerType shift) noexcept; constexpr byte& operator>>=(byte& b, IntegerType shift) noexcept;
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>> template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
constexpr byte operator>>(byte b, IntegerType shift) noexcept; constexpr byte operator>>(byte b, IntegerType shift) noexcept;
``` ```
@ -134,7 +134,7 @@ constexpr byte operator~(byte b) noexcept;
Bitwise negation of a `byte`. Flips all bits. Zeroes become ones, ones become zeroes. Bitwise negation of a `byte`. Flips all bits. Zeroes become ones, ones become zeroes.
```cpp ```cpp
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>> template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
constexpr IntegerType to_integer(byte b) noexcept; constexpr IntegerType to_integer(byte b) noexcept;
``` ```
@ -224,6 +224,14 @@ When a nullptr check fails, `std::terminate` is called.
See [F.23: Use a `not_null<T>` to indicate that “null” is not a valid value](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-nullptr) See [F.23: Use a `not_null<T>` to indicate that “null” is not a valid value](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-nullptr)
#### Member Types
```cpp
using element_type = T;
```
The type of the pointer or smart pointer that is managed by this object.
#### Member functions #### Member functions
##### Construct/Copy ##### Construct/Copy
@ -294,6 +302,12 @@ void operator[](std::ptrdiff_t) const = delete;
Array index operator is explicitly deleted. Pointers point to single objects ([I.13: Do not pass an array as a single pointer](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Ri-array)), so don't allow treating them as an array. Array index operator is explicitly deleted. Pointers point to single objects ([I.13: Do not pass an array as a single pointer](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Ri-array)), so don't allow treating them as an array.
```cpp
void swap(not_null<T>& other) { std::swap(ptr_, other.ptr_); }
```
Swaps contents with another `gsl::not_null` object.
#### Non-member functions #### Non-member functions
```cpp ```cpp
@ -303,6 +317,13 @@ auto make_not_null(T&& t) noexcept;
Creates a `gsl::not_null` object, deducing the target type from the type of the argument. Creates a `gsl::not_null` object, deducing the target type from the type of the argument.
```cpp
template <typename T, std::enable_if_t<std::is_move_assignable<T>::value && std::is_move_constructible<T>::value, bool> = true>
void swap(not_null<T>& a, not_null<T>& b);
```
Swaps the contents of two `gsl::not_null` objects.
```cpp ```cpp
template <class T, class U> template <class T, class U>
auto operator==(const not_null<T>& lhs, auto operator==(const not_null<T>& lhs,
@ -663,10 +684,6 @@ template <class Container>
constexpr span<typename Container::value_type> make_span(Container& cont); constexpr span<typename Container::value_type> make_span(Container& cont);
template <class Container> template <class Container>
constexpr span<const typename Container::value_type> make_span(const Container& cont); constexpr span<const typename Container::value_type> make_span(const Container& cont);
template <class Ptr>
constexpr span<typename Ptr::element_type> make_span(Ptr& cont, std::size_t count);
template <class Ptr>
constexpr span<typename Ptr::element_type> make_span(Ptr& cont);
``` ```
Utility function for creating a `span` with [`gsl::dynamic_extent`](#user-content-H-span_ext-dynamic_extent) from Utility function for creating a `span` with [`gsl::dynamic_extent`](#user-content-H-span_ext-dynamic_extent) from
@ -858,3 +875,10 @@ constexpr auto at(std::span<T, extent> sp, const index i) -> decltype(sp[sp.size
This overload returns a reference to the `i`s element of the `std::span` `sp`. It [`Expects`](#user-content-H-assert-expects) that the provided index is within the bounds of the array. This overload returns a reference to the `i`s element of the `std::span` `sp`. It [`Expects`](#user-content-H-assert-expects) that the provided index is within the bounds of the array.
For [`gsl::at`](#user-content-H-span_ext-at) for [`gsl::span`](#user-content-H-span-span) see header [`span_ext`](#user-content-H-span_ext). For [`gsl::at`](#user-content-H-span_ext-at) for [`gsl::span`](#user-content-H-span-span) see header [`span_ext`](#user-content-H-span_ext).
```cpp
template <class T, std::enable_if_t<std::is_move_assignable<T>::value && std::is_move_constructible<T>::value>>
void swap(T& a, T& b);
```
Swaps the contents of two objects. Exists only to specialize `gsl::swap<T>(gsl::not_null<T>&, gsl::not_null<T>&)`.

View File

@ -17,8 +17,8 @@
#ifndef GSL_ALGORITHM_H #ifndef GSL_ALGORITHM_H
#define GSL_ALGORITHM_H #define GSL_ALGORITHM_H
#include "assert" // for Expects #include "./assert" // for Expects
#include "span" // for dynamic_extent, span #include "./span" // for dynamic_extent, span
#include <algorithm> // for copy_n #include <algorithm> // for copy_n
#include <cstddef> // for ptrdiff_t #include <cstddef> // for ptrdiff_t

View File

@ -17,6 +17,8 @@
#ifndef GSL_BYTE_H #ifndef GSL_BYTE_H
#define GSL_BYTE_H #define GSL_BYTE_H
#include "./util" // for GSL_DEPRECATED
#include <type_traits> #include <type_traits>
#ifdef _MSC_VER #ifdef _MSC_VER
@ -80,7 +82,14 @@ namespace gsl
{ {
#if GSL_USE_STD_BYTE #if GSL_USE_STD_BYTE
using std::byte; namespace impl {
// impl::byte is used by gsl::as_bytes so our own code does not trigger a deprecation warning as would be the case when we used gsl::byte.
// Users of GSL should only use gsl::byte, not gsl::impl::byte.
using byte = std::byte;
}
using byte GSL_DEPRECATED("Use std::byte instead.") = std::byte;
using std::to_integer; using std::to_integer;
#else // GSL_USE_STD_BYTE #else // GSL_USE_STD_BYTE
@ -91,25 +100,31 @@ enum class byte_may_alias byte : unsigned char
{ {
}; };
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>> namespace impl {
// impl::byte is used by gsl::as_bytes so our own code does not trigger a deprecation warning as would be the case when we used gsl::byte.
// Users of GSL should only use gsl::byte, not gsl::impl::byte.
using byte = gsl::byte;
}
template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
constexpr byte& operator<<=(byte& b, IntegerType shift) noexcept constexpr byte& operator<<=(byte& b, IntegerType shift) noexcept
{ {
return b = byte(static_cast<unsigned char>(b) << shift); return b = byte(static_cast<unsigned char>(b) << shift);
} }
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>> template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
constexpr byte operator<<(byte b, IntegerType shift) noexcept constexpr byte operator<<(byte b, IntegerType shift) noexcept
{ {
return byte(static_cast<unsigned char>(b) << shift); return byte(static_cast<unsigned char>(b) << shift);
} }
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>> template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
constexpr byte& operator>>=(byte& b, IntegerType shift) noexcept constexpr byte& operator>>=(byte& b, IntegerType shift) noexcept
{ {
return b = byte(static_cast<unsigned char>(b) >> shift); return b = byte(static_cast<unsigned char>(b) >> shift);
} }
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>> template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
constexpr byte operator>>(byte b, IntegerType shift) noexcept constexpr byte operator>>(byte b, IntegerType shift) noexcept
{ {
return byte(static_cast<unsigned char>(b) >> shift); return byte(static_cast<unsigned char>(b) >> shift);
@ -147,7 +162,7 @@ constexpr byte operator^(byte l, byte r) noexcept
constexpr byte operator~(byte b) noexcept { return byte(~static_cast<unsigned char>(b)); } constexpr byte operator~(byte b) noexcept { return byte(~static_cast<unsigned char>(b)); }
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>> template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
constexpr IntegerType to_integer(byte b) noexcept constexpr IntegerType to_integer(byte b) noexcept
{ {
return static_cast<IntegerType>(b); return static_cast<IntegerType>(b);
@ -155,23 +170,24 @@ constexpr IntegerType to_integer(byte b) noexcept
#endif // GSL_USE_STD_BYTE #endif // GSL_USE_STD_BYTE
template <typename T> template <typename T>
// NOTE: need suppression since c++14 does not allow "return {t}" // NOTE: need suppression since c++14 does not allow "return {t}"
// GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: suppression does not work // GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: suppression does not work
constexpr byte to_byte(T t) noexcept constexpr gsl::impl::byte to_byte(T t) noexcept
{ {
static_assert(std::is_same<T, unsigned char>::value, static_assert(std::is_same<T, unsigned char>::value,
"gsl::to_byte(t) must be provided an unsigned char, otherwise data loss may occur. " "gsl::to_byte(t) must be provided an unsigned char, otherwise data loss may occur. "
"If you are calling to_byte with an integer constant use: gsl::to_byte<t>() version."); "If you are calling to_byte with an integer constant use: gsl::to_byte<t>() version.");
return byte(t); return gsl::impl::byte(t);
} }
template <int I> template <int I>
constexpr byte to_byte() noexcept constexpr gsl::impl::byte to_byte() noexcept
{ {
static_assert(I >= 0 && I <= 255, static_assert(I >= 0 && I <= 255,
"gsl::byte only has 8 bits of storage, values must be in range 0-255"); "gsl::byte only has 8 bits of storage, values must be in range 0-255");
return static_cast<byte>(I); return static_cast<gsl::impl::byte>(I);
} }
} // namespace gsl } // namespace gsl

View File

@ -18,16 +18,16 @@
#define GSL_GSL_H #define GSL_GSL_H
// IWYU pragma: begin_exports // IWYU pragma: begin_exports
#include "algorithm" // copy #include "./algorithm" // copy
#include "assert" // Ensures/Expects #include "./assert" // Ensures/Expects
#include "byte" // byte #include "./byte" // byte
#include "pointers" // owner, not_null #include "./pointers" // owner, not_null
#include "span" // span #include "./span" // span
#include "zstring" // zstring #include "./zstring" // zstring
#include "util" // finally()/narrow_cast()... #include "./util" // finally()/narrow_cast()...
#ifdef __cpp_exceptions #ifdef __cpp_exceptions
#include "narrow" // narrow() #include "./narrow" // narrow()
#endif #endif
// IWYU pragma: end_exports // IWYU pragma: end_exports

View File

@ -16,8 +16,8 @@
#ifndef GSL_NARROW_H #ifndef GSL_NARROW_H
#define GSL_NARROW_H #define GSL_NARROW_H
#include "assert" // for GSL_SUPPRESS #include "./assert" // for GSL_SUPPRESS
#include "util" // for narrow_cast #include "./util" // for narrow_cast
#include <exception> // for std::exception #include <exception> // for std::exception
namespace gsl namespace gsl
{ {

View File

@ -17,7 +17,8 @@
#ifndef GSL_POINTERS_H #ifndef GSL_POINTERS_H
#define GSL_POINTERS_H #define GSL_POINTERS_H
#include "assert" // for Ensures, Expects #include "./assert" // for Ensures, Expects
#include "./util" // for GSL_DEPRECATED
#include <cstddef> // for ptrdiff_t, nullptr_t, size_t #include <cstddef> // for ptrdiff_t, nullptr_t, size_t
#include <functional> // for less, greater #include <functional> // for less, greater
@ -62,8 +63,11 @@ namespace details
// //
// GSL.owner: ownership pointers // GSL.owner: ownership pointers
// //
using std::shared_ptr; template <typename... Ts>
using std::unique_ptr; using shared_ptr GSL_DEPRECATED("Use std::shared_ptr instead") = std::shared_ptr<Ts...>;
template <typename... Ts>
using unique_ptr GSL_DEPRECATED("Use std::unique_ptr instead") = std::unique_ptr<Ts...>;
// //
// owner // owner
@ -75,7 +79,7 @@ using std::unique_ptr;
// T must be a pointer type // T must be a pointer type
// - disallow construction from any type other than pointer type // - disallow construction from any type other than pointer type
// //
template <class T, class = std::enable_if_t<std::is_pointer<T>::value>> template <class T, std::enable_if_t<std::is_pointer<T>::value, bool> = true>
using owner = T; using owner = T;
// //
@ -98,6 +102,8 @@ class not_null
public: public:
static_assert(details::is_comparable_to_nullptr<T>::value, "T cannot be compared to nullptr."); static_assert(details::is_comparable_to_nullptr<T>::value, "T cannot be compared to nullptr.");
using element_type = T;
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>> template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
constexpr not_null(U&& u) noexcept(std::is_nothrow_move_constructible<T>::value) : ptr_(std::forward<U>(u)) constexpr not_null(U&& u) noexcept(std::is_nothrow_move_constructible<T>::value) : ptr_(std::forward<U>(u))
{ {
@ -117,7 +123,7 @@ public:
not_null(const not_null& other) = default; not_null(const not_null& other) = default;
not_null& operator=(const not_null& other) = default; not_null& operator=(const not_null& other) = default;
constexpr details::value_or_reference_return_t<T> get() const constexpr details::value_or_reference_return_t<T> get() const
noexcept(noexcept(details::value_or_reference_return_t<T>{std::declval<T&>()})) noexcept(noexcept(details::value_or_reference_return_t<T>(std::declval<T&>())))
{ {
return ptr_; return ptr_;
} }
@ -139,10 +145,18 @@ public:
not_null& operator-=(std::ptrdiff_t) = delete; not_null& operator-=(std::ptrdiff_t) = delete;
void operator[](std::ptrdiff_t) const = delete; void operator[](std::ptrdiff_t) const = delete;
void swap(not_null<T>& other) { std::swap(ptr_, other.ptr_); }
private: private:
T ptr_; T ptr_;
}; };
template <typename T, std::enable_if_t<std::is_move_assignable<T>::value && std::is_move_constructible<T>::value, bool> = true>
void swap(not_null<T>& a, not_null<T>& b)
{
a.swap(b);
}
template <class T> template <class T>
auto make_not_null(T&& t) noexcept auto make_not_null(T&& t) noexcept
{ {
@ -267,19 +281,19 @@ class strict_not_null : public not_null<T>
{ {
public: public:
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>> template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
constexpr explicit strict_not_null(U&& u) : not_null<T>(std::forward<U>(u)) constexpr explicit strict_not_null(U&& u) noexcept(std::is_nothrow_move_constructible<T>::value) : not_null<T>(std::forward<U>(u))
{} {}
template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>> template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
constexpr explicit strict_not_null(T u) : not_null<T>(u) constexpr explicit strict_not_null(T u) noexcept(std::is_nothrow_move_constructible<T>::value) : not_null<T>(std::move(u))
{} {}
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>> template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
constexpr strict_not_null(const not_null<U>& other) : not_null<T>(other) constexpr strict_not_null(const not_null<U>& other) noexcept(std::is_nothrow_move_constructible<T>::value) : not_null<T>(other)
{} {}
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>> template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
constexpr strict_not_null(const strict_not_null<U>& other) : not_null<T>(other) constexpr strict_not_null(const strict_not_null<U>& other) noexcept(std::is_nothrow_move_constructible<T>::value) : not_null<T>(other)
{} {}
// To avoid invalidating the "not null" invariant, the contained pointer is actually copied // To avoid invalidating the "not null" invariant, the contained pointer is actually copied

View File

@ -17,10 +17,10 @@
#ifndef GSL_SPAN_H #ifndef GSL_SPAN_H
#define GSL_SPAN_H #define GSL_SPAN_H
#include "assert" // for Expects #include "./assert" // for Expects
#include "byte" // for byte #include "./byte" // for gsl::impl::byte
#include "span_ext" // for span specialization of gsl::at and other span-related extensions #include "./span_ext" // for span specialization of gsl::at and other span-related extensions
#include "util" // for narrow_cast #include "./util" // for narrow_cast
#include <array> // for array #include <array> // for array
#include <cstddef> // for ptrdiff_t, size_t, nullptr_t #include <cstddef> // for ptrdiff_t, size_t, nullptr_t
@ -140,7 +140,9 @@ namespace details
constexpr span_iterator(pointer begin, pointer end, pointer current) constexpr span_iterator(pointer begin, pointer end, pointer current)
: begin_(begin), end_(end), current_(current) : begin_(begin), end_(end), current_(current)
{} {
Expects(begin_ <= current_ && current <= end_);
}
constexpr operator span_iterator<const Type>() const noexcept constexpr operator span_iterator<const Type>() const noexcept
{ {
@ -149,21 +151,18 @@ namespace details
constexpr reference operator*() const noexcept constexpr reference operator*() const noexcept
{ {
Expects(begin_ && end_); Expects(current_ != end_);
Expects(begin_ <= current_ && current_ < end_);
return *current_; return *current_;
} }
constexpr pointer operator->() const noexcept constexpr pointer operator->() const noexcept
{ {
Expects(begin_ && end_); Expects(current_ != end_);
Expects(begin_ <= current_ && current_ < end_);
return current_; return current_;
} }
constexpr span_iterator& operator++() noexcept constexpr span_iterator& operator++() noexcept
{ {
Expects(begin_ && current_ && end_); Expects(current_ != end_);
Expects(current_ < end_);
// clang-format off // clang-format off
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
// clang-format on // clang-format on
@ -180,8 +179,7 @@ namespace details
constexpr span_iterator& operator--() noexcept constexpr span_iterator& operator--() noexcept
{ {
Expects(begin_ && end_); Expects(begin_ != current_);
Expects(begin_ < current_);
--current_; --current_;
return *this; return *this;
} }
@ -584,6 +582,8 @@ public:
template <std::size_t Count> template <std::size_t Count>
constexpr span<element_type, Count> first() const noexcept constexpr span<element_type, Count> first() const noexcept
{ {
static_assert(Extent == dynamic_extent || Count <= Extent,
"first() cannot extract more elements from a span than it contains.");
Expects(Count <= size()); Expects(Count <= size());
return span<element_type, Count>{data(), Count}; return span<element_type, Count>{data(), Count};
} }
@ -594,6 +594,8 @@ public:
// clang-format on // clang-format on
constexpr span<element_type, Count> last() const noexcept constexpr span<element_type, Count> last() const noexcept
{ {
static_assert(Extent == dynamic_extent || Count <= Extent,
"last() cannot extract more elements from a span than it contains.");
Expects(Count <= size()); Expects(Count <= size());
return span<element_type, Count>{data() + (size() - Count), Count}; return span<element_type, Count>{data() + (size() - Count), Count};
} }
@ -605,6 +607,9 @@ public:
constexpr auto subspan() const noexcept -> constexpr auto subspan() const noexcept ->
typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type
{ {
static_assert(Extent == dynamic_extent || (Extent >= Offset && (Count == dynamic_extent ||
Count <= Extent - Offset)),
"subspan() cannot extract more elements from a span than it contains.");
Expects((size() >= Offset) && (Count == dynamic_extent || (Count <= size() - Offset))); Expects((size() >= Offset) && (Count == dynamic_extent || (Count <= size() - Offset)));
using type = using type =
typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type; typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type;
@ -819,28 +824,28 @@ namespace details
// [span.objectrep], views of object representation // [span.objectrep], views of object representation
template <class ElementType, std::size_t Extent> template <class ElementType, std::size_t Extent>
span<const byte, details::calculate_byte_size<ElementType, Extent>::value> span<const gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>
as_bytes(span<ElementType, Extent> s) noexcept as_bytes(span<ElementType, Extent> s) noexcept
{ {
using type = span<const byte, details::calculate_byte_size<ElementType, Extent>::value>; using type = span<const gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>;
// clang-format off // clang-format off
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
// clang-format on // clang-format on
return type{reinterpret_cast<const byte*>(s.data()), s.size_bytes()}; return type{reinterpret_cast<const gsl::impl::byte*>(s.data()), s.size_bytes()};
} }
template <class ElementType, std::size_t Extent, template <class ElementType, std::size_t Extent,
std::enable_if_t<!std::is_const<ElementType>::value, int> = 0> std::enable_if_t<!std::is_const<ElementType>::value, int> = 0>
span<byte, details::calculate_byte_size<ElementType, Extent>::value> span<gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>
as_writable_bytes(span<ElementType, Extent> s) noexcept as_writable_bytes(span<ElementType, Extent> s) noexcept
{ {
using type = span<byte, details::calculate_byte_size<ElementType, Extent>::value>; using type = span<gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>;
// clang-format off // clang-format off
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
// clang-format on // clang-format on
return type{reinterpret_cast<byte*>(s.data()), s.size_bytes()}; return type{reinterpret_cast<gsl::impl::byte*>(s.data()), s.size_bytes()};
} }
} // namespace gsl } // namespace gsl

View File

@ -27,8 +27,8 @@
// //
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#include "assert" // GSL_KERNEL_MODE #include "./assert" // GSL_KERNEL_MODE
#include "util" // for narrow_cast, narrow #include "./util" // for narrow_cast, narrow
#include <cstddef> // for ptrdiff_t, size_t #include <cstddef> // for ptrdiff_t, size_t
#include <utility> #include <utility>
@ -123,14 +123,14 @@ constexpr span<const typename Container::value_type> make_span(const Container&
} }
template <class Ptr> template <class Ptr>
[[deprecated("This function is deprecated. See GSL issue #1092.")]] GSL_DEPRECATED("This function is deprecated. See GSL issue #1092.")
constexpr span<typename Ptr::element_type> make_span(Ptr& cont, std::size_t count) constexpr span<typename Ptr::element_type> make_span(Ptr& cont, std::size_t count)
{ {
return span<typename Ptr::element_type>(cont, count); return span<typename Ptr::element_type>(cont, count);
} }
template <class Ptr> template <class Ptr>
[[deprecated("This function is deprecated. See GSL issue #1092.")]] GSL_DEPRECATED("This function is deprecated. See GSL issue #1092.")
constexpr span<typename Ptr::element_type> make_span(Ptr& cont) constexpr span<typename Ptr::element_type> make_span(Ptr& cont)
{ {
return span<typename Ptr::element_type>(cont); return span<typename Ptr::element_type>(cont);

View File

@ -1,4 +0,0 @@
#pragma once
#pragma message( \
"This header will soon be removed. Use <gsl/zstring> instead of <gsl/string_span>")
#include "zstring"

View File

@ -17,7 +17,7 @@
#ifndef GSL_UTIL_H #ifndef GSL_UTIL_H
#define GSL_UTIL_H #define GSL_UTIL_H
#include "assert" // for Expects #include "./assert" // for Expects
#include <array> #include <array>
#include <cstddef> // for ptrdiff_t, size_t #include <cstddef> // for ptrdiff_t, size_t
@ -60,6 +60,32 @@
#define GSL_INLINE #define GSL_INLINE
#endif #endif
#if defined(__has_cpp_attribute)
#if __has_cpp_attribute(deprecated)
#define GSL_DEPRECATED(msg) [[deprecated(msg)]]
#endif // __has_cpp_attribute(deprecated)
#endif // defined(__has_cpp_attribute)
#if !defined(GSL_DEPRECATED)
#if defined(__cplusplus)
#if __cplusplus >= 201309L
#define GSL_DEPRECATED(msg) [[deprecated(msg)]]
#endif // __cplusplus >= 201309L
#endif // defined(__cplusplus)
#endif // !defined(GSL_DEPRECATED)
#if !defined(GSL_DEPRECATED)
#if defined(_MSC_VER)
#define GSL_DEPRECATED(msg) __declspec(deprecated(msg))
#elif defined(__GNUC__)
#define GSL_DEPRECATED(msg) __attribute__((deprecated(msg)))
#endif // defined(_MSC_VER)
#endif // !defined(GSL_DEPRECATED)
#if !defined(GSL_DEPRECATED)
#define GSL_DEPRECATED(msg)
#endif // !defined(GSL_DEPRECATED)
namespace gsl namespace gsl
{ {
// //
@ -146,6 +172,9 @@ GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
return *(cont.begin() + i); return *(cont.begin() + i);
} }
template <class T, std::enable_if_t<std::is_move_assignable<T>::value && std::is_move_constructible<T>::value>>
void swap(T& a, T& b) { std::swap(a, b); }
#if defined(__cpp_lib_span) && __cpp_lib_span >= 202002L #if defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
template <class T, std::size_t extent = std::dynamic_extent> template <class T, std::size_t extent = std::dynamic_extent>
constexpr auto at(std::span<T, extent> sp, const index i) -> decltype(sp[sp.size()]) constexpr auto at(std::span<T, extent> sp, const index i) -> decltype(sp[sp.size()])

View File

@ -17,7 +17,7 @@
#ifndef GSL_ZSTRING_H #ifndef GSL_ZSTRING_H
#define GSL_ZSTRING_H #define GSL_ZSTRING_H
#include "span_ext" // for dynamic_extent #include "./span_ext" // for dynamic_extent
#include <cstddef> // for size_t, nullptr_t #include <cstddef> // for size_t, nullptr_t

View File

@ -110,6 +110,7 @@ if(MSVC) # MSVC or simulating MSVC
-Wno-shift-sign-overflow # GTest gtest-port.h -Wno-shift-sign-overflow # GTest gtest-port.h
-Wno-undef # GTest -Wno-undef # GTest
-Wno-used-but-marked-unused # GTest EXPECT_DEATH -Wno-used-but-marked-unused # GTest EXPECT_DEATH
-Wno-switch-default # GTest EXPECT_DEATH
$<$<EQUAL:${GSL_CXX_STANDARD},14>: # no support for [[maybe_unused]] $<$<EQUAL:${GSL_CXX_STANDARD},14>: # no support for [[maybe_unused]]
-Wno-unused-member-function -Wno-unused-member-function
-Wno-unused-variable -Wno-unused-variable
@ -205,6 +206,7 @@ add_executable(gsl_tests
byte_tests.cpp byte_tests.cpp
notnull_tests.cpp notnull_tests.cpp
owner_tests.cpp owner_tests.cpp
pointers_tests.cpp
span_compatibility_tests.cpp span_compatibility_tests.cpp
span_ext_tests.cpp span_ext_tests.cpp
span_tests.cpp span_tests.cpp

View File

@ -1,10 +1,10 @@
cmake_minimum_required(VERSION 3.0.2) cmake_minimum_required(VERSION 3.13)
project(googletest-download NONE) project(googletest-download NONE)
include(ExternalProject) include(ExternalProject)
ExternalProject_Add(googletest ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG 1b18723e874b256c1e39378c6774a90701d70f7a GIT_TAG v1.14.0
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src" SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build" BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND "" CONFIGURE_COMMAND ""

View File

@ -188,7 +188,7 @@ TEST(algorithm_tests, incompatible_type)
span<int> src_span_dyn(src); span<int> src_span_dyn(src);
span<int, 4> src_span_static(src); span<int, 4> src_span_static(src);
span<int*> dst_span_dyn(dst); span<int*> dst_span_dyn(dst);
span<int*, 4> dst_span_static(dst); span<int*, 4> dst_span_static(gsl::make_span(dst));
// every line should produce a compilation error // every line should produce a compilation error
copy(src_span_dyn, dst_span_dyn); copy(src_span_dyn, dst_span_dyn);

View File

@ -16,8 +16,12 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#define GSL_USE_STD_BYTE 0
#include <gsl/byte> // for to_byte, to_integer, byte, operator&, ope... #include <gsl/byte> // for to_byte, to_integer, byte, operator&, ope...
#include <type_traits>
#include <utility>
using namespace std; using namespace std;
using namespace gsl; using namespace gsl;
@ -33,28 +37,28 @@ int modify_both(gsl::byte& b, int& i)
TEST(byte_tests, construction) TEST(byte_tests, construction)
{ {
{ {
const byte b = static_cast<byte>(4); const gsl::byte b = static_cast<gsl::byte>(4);
EXPECT_TRUE(static_cast<unsigned char>(b) == 4); EXPECT_TRUE(static_cast<unsigned char>(b) == 4);
} }
{ {
const byte b = byte(12); const gsl::byte b = gsl::byte(12);
EXPECT_TRUE(static_cast<unsigned char>(b) == 12); EXPECT_TRUE(static_cast<unsigned char>(b) == 12);
} }
{ {
const byte b = to_byte<12>(); const gsl::byte b = to_byte<12>();
EXPECT_TRUE(static_cast<unsigned char>(b) == 12); EXPECT_TRUE(static_cast<unsigned char>(b) == 12);
} }
{ {
const unsigned char uc = 12; const unsigned char uc = 12;
const byte b = to_byte(uc); const gsl::byte b = to_byte(uc);
EXPECT_TRUE(static_cast<unsigned char>(b) == 12); EXPECT_TRUE(static_cast<unsigned char>(b) == 12);
} }
#if defined(__cplusplus) && (__cplusplus >= 201703L) #if defined(__cplusplus) && (__cplusplus >= 201703L)
{ {
const byte b{14}; const gsl::byte b{14};
EXPECT_TRUE(static_cast<unsigned char>(b) == 14); EXPECT_TRUE(static_cast<unsigned char>(b) == 14);
} }
#endif #endif
@ -63,14 +67,16 @@ TEST(byte_tests, construction)
to_byte(char{}); to_byte(char{});
to_byte(3); to_byte(3);
to_byte(3u); to_byte(3u);
to_byte<-1>();
to_byte<256u>();
#endif #endif
} }
TEST(byte_tests, bitwise_operations) TEST(byte_tests, bitwise_operations)
{ {
const byte b = to_byte<0xFF>(); const gsl::byte b = to_byte<0xFF>();
byte a = to_byte<0x00>(); gsl::byte a = to_byte<0x00>();
EXPECT_TRUE((b | a) == to_byte<0xFF>()); EXPECT_TRUE((b | a) == to_byte<0xFF>());
EXPECT_TRUE(a == to_byte<0x00>()); EXPECT_TRUE(a == to_byte<0x00>());
@ -104,7 +110,7 @@ TEST(byte_tests, bitwise_operations)
TEST(byte_tests, to_integer) TEST(byte_tests, to_integer)
{ {
const byte b = to_byte<0x12>(); const gsl::byte b = to_byte<0x12>();
EXPECT_TRUE(0x12 == gsl::to_integer<char>(b)); EXPECT_TRUE(0x12 == gsl::to_integer<char>(b));
EXPECT_TRUE(0x12 == gsl::to_integer<short>(b)); EXPECT_TRUE(0x12 == gsl::to_integer<short>(b));
@ -123,12 +129,50 @@ TEST(byte_tests, to_integer)
TEST(byte_tests, aliasing) TEST(byte_tests, aliasing)
{ {
int i{0}; int i{0};
const int res = modify_both(reinterpret_cast<byte&>(i), i); const int res = modify_both(reinterpret_cast<gsl::byte&>(i), i);
EXPECT_TRUE(res == i); EXPECT_TRUE(res == i);
} }
} // namespace #if __cplusplus >= 201703l
using std::void_t;
#else // __cplusplus >= 201703l
template <class...>
using void_t = void;
#endif // __cplusplus < 201703l
#ifdef CONFIRM_COMPILATION_ERRORS template <typename U, typename = void>
copy(src_span_static, dst_span_static); static constexpr bool LShiftCompilesFor = false;
#endif template <typename U>
static constexpr bool LShiftCompilesFor<
U, void_t<decltype(gsl::operator<< <float>(declval<gsl::byte>(), declval<U>()))>> = true;
static_assert(!LShiftCompilesFor<float>, "!LShiftCompilesFor<float>");
template <typename U, typename = void>
static constexpr bool RShiftCompilesFor = false;
template <typename U>
static constexpr bool RShiftCompilesFor<
U, void_t<decltype(gsl::operator>> <U>(declval<gsl::byte>(), declval<U>()))>> = true;
static_assert(!RShiftCompilesFor<float>, "!RShiftCompilesFor<float>");
template <typename U, typename = void>
static constexpr bool LShiftAssignCompilesFor = false;
template <typename U>
static constexpr bool LShiftAssignCompilesFor<
U, void_t<decltype(gsl::operator<<= <U>(declval<gsl::byte&>(), declval<U>()))>> = true;
static_assert(!LShiftAssignCompilesFor<float>, "!LShiftAssignCompilesFor<float>");
template <typename U, typename = void>
static constexpr bool RShiftAssignCompilesFor = false;
template <typename U>
static constexpr bool RShiftAssignCompilesFor<
U, void_t<decltype(gsl::operator>>= <U>(declval<gsl::byte&>(), declval<U>()))>> = true;
static_assert(!RShiftAssignCompilesFor<float>, "!RShiftAssignCompilesFor<float>");
template <typename U, typename = void>
static constexpr bool ToIntegerCompilesFor = false;
template <typename U>
static constexpr bool
ToIntegerCompilesFor<U, void_t<decltype(gsl::to_integer<U>(gsl::byte{}))>> = true;
static_assert(!ToIntegerCompilesFor<float>, "!ToIntegerCompilesFor<float>");
} // namespace

View File

@ -18,17 +18,25 @@
#include <gsl/pointers> // for not_null, operator<, operator<=, operator> #include <gsl/pointers> // for not_null, operator<, operator<=, operator>
#include <algorithm> // for addressof #include <algorithm> // for addressof
#include <cstdint> // for uint16_t #include <cstdint> // for uint16_t
#include <memory> // for shared_ptr, make_shared, operator<, opera... #include <memory> // for shared_ptr, make_shared, operator<, opera...
#include <sstream> // for operator<<, ostringstream, basic_ostream:... #include <sstream> // for operator<<, ostringstream, basic_ostream:...
#include <string> // for basic_string, operator==, string, operator<< #include <string> // for basic_string, operator==, string, operator<<
#include <typeinfo> // for type_info #include <type_traits> // for declval
#include <variant> // for variant, monostate, get #include <typeinfo> // for type_info
#include <variant> // for variant, monostate, get
#include "deathTestCommon.h" #include "deathTestCommon.h"
using namespace gsl; using namespace gsl;
#if __cplusplus >= 201703l
using std::void_t;
#else // __cplusplus >= 201703l
template <class...>
using void_t = void;
#endif // __cplusplus < 201703l
struct MyBase struct MyBase
{ {
}; };
@ -141,16 +149,39 @@ bool helper_const(not_null<const int*> p) { return *p == 12; }
int* return_pointer() { return nullptr; } int* return_pointer() { return nullptr; }
} // namespace } // namespace
template <typename U, typename = void>
static constexpr bool CtorCompilesFor_A = false;
template <typename U>
static constexpr bool
CtorCompilesFor_A<U, void_t<decltype(gsl::not_null<void*>{std::declval<U>()})>> = true;
template <typename U, int N, typename = void>
static constexpr bool CtorCompilesFor_B = false;
template <typename U, int N>
static constexpr bool CtorCompilesFor_B<U, N, void_t<decltype(gsl::not_null<U>{N})>> = true;
template <typename U, typename = void>
static constexpr bool DefaultCtorCompilesFor = false;
template <typename U>
static constexpr bool DefaultCtorCompilesFor<U, void_t<decltype(gsl::not_null<U>{})>> = true;
template <typename U, typename = void>
static constexpr bool CtorCompilesFor_C = false;
template <typename U>
static constexpr bool
CtorCompilesFor_C<U, void_t<decltype(gsl::not_null<U*>{std::declval<std::unique_ptr<U>>()})>> =
true;
TEST(notnull_tests, TestNotNullConstructors) TEST(notnull_tests, TestNotNullConstructors)
{ {
{ {
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(CtorCompilesFor_A<void*>, "CtorCompilesFor_A<void*>");
not_null<int*> p = nullptr; // yay...does not compile! static_assert(!CtorCompilesFor_A<std::nullptr_t>, "!CtorCompilesFor_A<std::nullptr_t>");
not_null<std::vector<char>*> p1 = 0; // yay...does not compile! static_assert(!CtorCompilesFor_B<void*, 0>, "!CtorCompilesFor_B<void*, 0>");
not_null<int*> p2; // yay...does not compile! static_assert(!DefaultCtorCompilesFor<void*>, "!DefaultCtorCompilesFor<void*>");
std::unique_ptr<int> up = std::make_unique<int>(120); static_assert(!CtorCompilesFor_C<int>, "CtorCompilesFor_C<int>");
not_null<int*> p3 = up;
#ifdef CONFIRM_COMPILATION_ERRORS
// Forbid non-nullptr assignable types // Forbid non-nullptr assignable types
not_null<std::vector<int>> f(std::vector<int>{1}); not_null<std::vector<int>> f(std::vector<int>{1});
not_null<int> z(10); not_null<int> z(10);
@ -178,6 +209,14 @@ TEST(notnull_tests, TestNotNullConstructors)
EXPECT_DEATH((not_null<decltype(pi)>(pi)), expected); EXPECT_DEATH((not_null<decltype(pi)>(pi)), expected);
} }
{
// from unique pointer
not_null<std::unique_ptr<int>> x(
std::make_unique<int>(10)); // unique_ptr<int> is nullptr assignable
EXPECT_DEATH((not_null<std::unique_ptr<int>>(std::unique_ptr<int>{})), expected);
}
{ {
// from pointer to local // from pointer to local
int t = 42; int t = 42;
@ -268,6 +307,27 @@ TEST(notnull_tests, TestNotNullostream)
ostream_helper<std::string>("string"); ostream_helper<std::string>("string");
} }
template <typename U, typename V, typename = void>
static constexpr bool AssignmentCompilesFor = false;
template <typename U, typename V>
static constexpr bool
AssignmentCompilesFor<U, V,
void_t<decltype(std::declval<gsl::not_null<U*>&>().operator=(
std::declval<gsl::not_null<V*>&>()))>> = true;
template <typename U, typename V, typename = void>
static constexpr bool SCastCompilesFor = false;
template <typename U, typename V>
static constexpr bool
SCastCompilesFor<U, V, void_t<decltype(static_cast<U*>(std::declval<gsl::not_null<V*>&>()))>> =
true;
template <typename U, typename V, typename = void>
static constexpr bool RCastCompilesFor = false;
template <typename U, typename V>
static constexpr bool RCastCompilesFor<
U, V, void_t<decltype(reinterpret_cast<U*>(std::declval<gsl::not_null<V*>&>()))>> = true;
TEST(notnull_tests, TestNotNullCasting) TEST(notnull_tests, TestNotNullCasting)
{ {
MyBase base; MyBase base;
@ -280,15 +340,30 @@ TEST(notnull_tests, TestNotNullCasting)
q = p; // allowed with heterogeneous copy ctor q = p; // allowed with heterogeneous copy ctor
EXPECT_TRUE(q == p); EXPECT_TRUE(q == p);
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(AssignmentCompilesFor<MyBase, MyDerived>,
q = u; // no viable conversion possible between MyBase* and Unrelated* "AssignmentCompilesFor<MyBase, MyDerived>");
p = q; // not possible to implicitly convert MyBase* to MyDerived* static_assert(!AssignmentCompilesFor<MyBase, Unrelated>,
"!AssignmentCompilesFor<MyBase, Unrelated>");
static_assert(!AssignmentCompilesFor<Unrelated, MyDerived>,
"!AssignmentCompilesFor<Unrelated, MyDerived>");
static_assert(!AssignmentCompilesFor<MyDerived, MyBase>,
"!AssignmentCompilesFor<MyDerived, MyBase>");
static_assert(SCastCompilesFor<MyDerived, MyDerived>, "SCastCompilesFor<MyDerived, MyDerived>");
static_assert(SCastCompilesFor<MyBase, MyDerived>, "SCastCompilesFor<MyBase, MyDerived>");
static_assert(!SCastCompilesFor<MyDerived, MyBase>, "!SCastCompilesFor<MyDerived, MyBase>");
static_assert(!SCastCompilesFor<Unrelated, MyDerived>,
"!SCastCompilesFor<Unrelated, MyDerived>");
static_assert(!RCastCompilesFor<MyDerived, MyDerived>,
"!SCastCompilesFor<MyDerived, MyDerived>");
static_assert(!RCastCompilesFor<Unrelated, MyDerived>,
"!SCastCompilesFor<Unrelated, MyDerived>");
not_null<Unrelated*> r = p;
not_null<Unrelated*> s = reinterpret_cast<Unrelated*>(p);
#endif
not_null<Unrelated*> t(reinterpret_cast<Unrelated*>(p.get())); not_null<Unrelated*> t(reinterpret_cast<Unrelated*>(p.get()));
EXPECT_TRUE(reinterpret_cast<void*>(p.get()) == reinterpret_cast<void*>(t.get())); EXPECT_TRUE(reinterpret_cast<void*>(p.get()) == reinterpret_cast<void*>(t.get()));
(void) static_cast<MyDerived*>(p);
(void) static_cast<MyBase*>(p);
} }
TEST(notnull_tests, TestNotNullAssignment) TEST(notnull_tests, TestNotNullAssignment)
@ -430,6 +505,18 @@ TEST(notnull_tests, TestNotNullCustomPtrComparison)
#if defined(__cplusplus) && (__cplusplus >= 201703L) #if defined(__cplusplus) && (__cplusplus >= 201703L)
template <typename U, typename = void>
static constexpr bool TypeDeductionCtorCompilesFor = false;
template <typename U>
static constexpr bool
TypeDeductionCtorCompilesFor<U, void_t<decltype(not_null{std::declval<U>()})>> = true;
template <typename U, typename = void>
static constexpr bool TypeDeductionHelperCompilesFor = false;
template <typename U>
static constexpr bool
TypeDeductionHelperCompilesFor<U, void_t<decltype(helper(not_null{std::declval<U>()}))>> = true;
TEST(notnull_tests, TestNotNullConstructorTypeDeduction) TEST(notnull_tests, TestNotNullConstructorTypeDeduction)
{ {
{ {
@ -446,9 +533,9 @@ TEST(notnull_tests, TestNotNullConstructorTypeDeduction)
const int i = 42; const int i = 42;
not_null x{&i}; not_null x{&i};
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(TypeDeductionHelperCompilesFor<int*>, "TypeDeductionHelperCompilesFor<int*>");
helper(not_null{&i}); static_assert(!TypeDeductionHelperCompilesFor<const int*>,
#endif "!TypeDeductionHelperCompilesFor<const int*>");
helper_const(not_null{&i}); helper_const(not_null{&i});
EXPECT_TRUE(*x == 42); EXPECT_TRUE(*x == 42);
@ -470,9 +557,6 @@ TEST(notnull_tests, TestNotNullConstructorTypeDeduction)
const int* p = &i; const int* p = &i;
not_null x{p}; not_null x{p};
#ifdef CONFIRM_COMPILATION_ERRORS
helper(not_null{p});
#endif
helper_const(not_null{p}); helper_const(not_null{p});
EXPECT_TRUE(*x == 42); EXPECT_TRUE(*x == 42);
@ -507,12 +591,15 @@ TEST(notnull_tests, TestNotNullConstructorTypeDeduction)
EXPECT_DEATH(helper_const(not_null{p}), expected); EXPECT_DEATH(helper_const(not_null{p}), expected);
} }
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(TypeDeductionCtorCompilesFor<void*>, "TypeDeductionCtorCompilesFor<void*>");
{ #if defined(_MSC_VER) && !defined(__clang__)
not_null x{nullptr}; // Fails on gcc, clang, xcode, VS clang with
helper(not_null{nullptr}); // "error : no type named 'type' in 'std::enable_if<false>'; 'enable_if' cannot be used to
helper_const(not_null{nullptr}); // disable this declaration"
} static_assert(!TypeDeductionCtorCompilesFor<std::nullptr_t>,
"!TypeDeductionCtorCompilesFor<std::nullptr_t>");
static_assert(!TypeDeductionHelperCompilesFor<std::nullptr_t>,
"!TypeDeductionHelperCompilesFor<std::nullptr_t>");
#endif #endif
} }
@ -528,6 +615,11 @@ TEST(notnull_tests, TestVariantEmplace)
} }
#endif // #if defined(__cplusplus) && (__cplusplus >= 201703L) #endif // #if defined(__cplusplus) && (__cplusplus >= 201703L)
template <typename U, typename = void>
static constexpr bool HelperCompilesFor = false;
template <typename U>
static constexpr bool HelperCompilesFor<U, void_t<decltype(helper(std::declval<U>()))>> = true;
TEST(notnull_tests, TestMakeNotNull) TEST(notnull_tests, TestMakeNotNull)
{ {
{ {
@ -544,9 +636,8 @@ TEST(notnull_tests, TestMakeNotNull)
const int i = 42; const int i = 42;
const auto x = make_not_null(&i); const auto x = make_not_null(&i);
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(HelperCompilesFor<gsl::not_null<int*>>,
helper(make_not_null(&i)); "HelperCompilesFor<gsl::not_null<int*>>");
#endif
helper_const(make_not_null(&i)); helper_const(make_not_null(&i));
EXPECT_TRUE(*x == 42); EXPECT_TRUE(*x == 42);
@ -568,9 +659,8 @@ TEST(notnull_tests, TestMakeNotNull)
const int* p = &i; const int* p = &i;
const auto x = make_not_null(p); const auto x = make_not_null(p);
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(!HelperCompilesFor<gsl::not_null<const int*>>,
helper(make_not_null(p)); "!HelperCompilesFor<gsl::not_null<const int*>>");
#endif
helper_const(make_not_null(p)); helper_const(make_not_null(p));
EXPECT_TRUE(*x == 42); EXPECT_TRUE(*x == 42);

View File

@ -17,6 +17,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <gsl/pointers> // for owner #include <gsl/pointers> // for owner
#include <type_traits> // for declval
using namespace gsl; using namespace gsl;
@ -32,12 +33,18 @@ TEST(owner_tests, basic_test)
delete p; delete p;
} }
TEST(owner_tests, check_pointer_constraint) #if __cplusplus >= 201703l
{ using std::void_t;
#ifdef CONFIRM_COMPILATION_ERRORS #else // __cplusplus >= 201703l
{ template <class...>
owner<int> integerTest = 10; using void_t = void;
owner<std::shared_ptr<int>> sharedPtrTest(new int(10)); #endif // __cplusplus < 201703l
}
#endif template <typename U, typename = void>
} static constexpr bool OwnerCompilesFor = false;
template <typename U>
static constexpr bool OwnerCompilesFor<U, void_t<decltype(gsl::owner<U>{})>> =
true;
static_assert(OwnerCompilesFor<int*>, "OwnerCompilesFor<int*>");
static_assert(!OwnerCompilesFor<int>, "!OwnerCompilesFor<int>");
static_assert(!OwnerCompilesFor<std::shared_ptr<int>>, "!OwnerCompilesFor<std::shared_ptr<int>>");

97
tests/pointers_tests.cpp Normal file
View File

@ -0,0 +1,97 @@
#include <gtest/gtest.h>
#include <gsl/pointers>
#include <memory>
#include <type_traits>
#include <utility>
#if __cplusplus >= 201703l
using std::void_t;
#else // __cplusplus >= 201703l
template <class...>
using void_t = void;
#endif // __cplusplus < 201703l
namespace
{
// Custom pointer type that can be used for gsl::not_null, but for which these cannot be swapped.
struct NotMoveAssignableCustomPtr
{
NotMoveAssignableCustomPtr() = default;
NotMoveAssignableCustomPtr(const NotMoveAssignableCustomPtr&) = default;
NotMoveAssignableCustomPtr& operator=(const NotMoveAssignableCustomPtr&) = default;
NotMoveAssignableCustomPtr(NotMoveAssignableCustomPtr&&) = default;
NotMoveAssignableCustomPtr& operator=(NotMoveAssignableCustomPtr&&) = delete;
bool operator!=(std::nullptr_t) const { return true; }
int dummy{}; // Without this clang warns, that NotMoveAssignableCustomPtr() is unneeded
};
template <typename U, typename = void>
static constexpr bool SwapCompilesFor = false;
template <typename U>
static constexpr bool
SwapCompilesFor<U, void_t<decltype(gsl::swap<U>(std::declval<gsl::not_null<U>&>(),
std::declval<gsl::not_null<U>&>()))>> = true;
TEST(pointers_test, swap)
{
// taken from gh-1129:
{
gsl::not_null<std::unique_ptr<int>> a(std::make_unique<int>(0));
gsl::not_null<std::unique_ptr<int>> b(std::make_unique<int>(1));
EXPECT_TRUE(*a == 0);
EXPECT_TRUE(*b == 1);
gsl::swap(a, b);
EXPECT_TRUE(*a == 1);
EXPECT_TRUE(*b == 0);
// Make sure our custom ptr can be used with not_null. The shared_pr is to prevent "unused"
// compiler warnings.
const auto shared_custom_ptr{std::make_shared<NotMoveAssignableCustomPtr>()};
gsl::not_null<NotMoveAssignableCustomPtr> c{*shared_custom_ptr};
EXPECT_TRUE(c.get() != nullptr);
}
{
gsl::strict_not_null<std::unique_ptr<int>> a{std::make_unique<int>(0)};
gsl::strict_not_null<std::unique_ptr<int>> b{std::make_unique<int>(1)};
EXPECT_TRUE(*a == 0);
EXPECT_TRUE(*b == 1);
gsl::swap(a, b);
EXPECT_TRUE(*a == 1);
EXPECT_TRUE(*b == 0);
}
{
gsl::not_null<std::unique_ptr<int>> a{std::make_unique<int>(0)};
gsl::strict_not_null<std::unique_ptr<int>> b{std::make_unique<int>(1)};
EXPECT_TRUE(*a == 0);
EXPECT_TRUE(*b == 1);
gsl::swap(a, b);
EXPECT_TRUE(*a == 1);
EXPECT_TRUE(*b == 0);
}
static_assert(!SwapCompilesFor<NotMoveAssignableCustomPtr>,
"!SwapCompilesFor<NotMoveAssignableCustomPtr>");
}
TEST(pointers_test, member_types)
{
static_assert(std::is_same<gsl::not_null<int*>::element_type, int*>::value,
"check member type: element_type");
}
} // namespace

View File

@ -1005,12 +1005,18 @@ static_assert(std::is_convertible<const std::array<int, 3>&, gsl::span<const int
"std::is_convertible<const std::array<int, 3>&, gsl::span<const int>>"); "std::is_convertible<const std::array<int, 3>&, gsl::span<const int>>");
#if __cplusplus >= 201703l #if __cplusplus >= 201703l
using std::void_t;
#else // __cplusplus >= 201703l
template <class...>
using void_t = void;
#endif // __cplusplus < 201703l
template <typename U, typename = void> template <typename U, typename = void>
static constexpr bool AsWritableBytesCompilesFor = false; static constexpr bool AsWritableBytesCompilesFor = false;
template <typename U> template <typename U>
static constexpr bool static constexpr bool
AsWritableBytesCompilesFor<U, void_t<decltype(as_writable_bytes(declval<U>()))>> = true; AsWritableBytesCompilesFor<U, ::void_t<decltype(as_writable_bytes(declval<U>()))>> = true;
static_assert(AsWritableBytesCompilesFor<gsl::span<int>>, static_assert(AsWritableBytesCompilesFor<gsl::span<int>>,
"AsWritableBytesCompilesFor<gsl::span<int>>"); "AsWritableBytesCompilesFor<gsl::span<int>>");
@ -1020,4 +1026,3 @@ static_assert(!AsWritableBytesCompilesFor<gsl::span<const int>>,
"!AsWritableBytesCompilesFor<gsl::span<const int>>"); "!AsWritableBytesCompilesFor<gsl::span<const int>>");
static_assert(!AsWritableBytesCompilesFor<gsl::span<const int, 9>>, static_assert(!AsWritableBytesCompilesFor<gsl::span<const int, 9>>,
"!AsWritableBytesCompilesFor<gsl::span<const int, 9>>"); "!AsWritableBytesCompilesFor<gsl::span<const int, 9>>");
#endif // __cplusplus >= 201703l

View File

@ -48,6 +48,13 @@
using namespace gsl; using namespace gsl;
#if __cplusplus >= 201703l
using std::void_t;
#else // __cplusplus >= 201703l
template <class...>
using void_t = void;
#endif // __cplusplus < 201703l
namespace namespace
{ {
@ -62,8 +69,7 @@ struct AddressOverloaded
#if (__cplusplus > 201402L) #if (__cplusplus > 201402L)
[[maybe_unused]] [[maybe_unused]]
#endif #endif
AddressOverloaded AddressOverloaded operator&() const
operator&() const
{ {
return {}; return {};
} }
@ -216,6 +222,12 @@ TEST(span_test, from_pointer_length_constructor)
TEST(span_test, from_pointer_pointer_construction) TEST(span_test, from_pointer_pointer_construction)
{ {
// const auto terminateHandler = std::set_terminate([] {
// std::cerr << "Expected Death. from_pointer_pointer_construction";
// std::abort();
// });
// const auto expected = GetExpectedDeathString(terminateHandler);
int arr[4] = {1, 2, 3, 4}; int arr[4] = {1, 2, 3, 4};
{ {
@ -245,19 +257,11 @@ TEST(span_test, from_pointer_pointer_construction)
EXPECT_TRUE(s.data() == &arr[0]); EXPECT_TRUE(s.data() == &arr[0]);
} }
// this will fail the std::distance() precondition, which asserts on MSVC debug builds //{ // this test succeeds on all platforms, gsl::span is more relaxed than std::span where this would be UB
//{
// auto workaround_macro = [&]() { span<int> s{&arr[1], &arr[0]}; }; // auto workaround_macro = [&]() { span<int> s{&arr[1], &arr[0]}; };
// EXPECT_DEATH(workaround_macro(), expected); // EXPECT_DEATH(workaround_macro(), expected);
//} //}
// this will fail the std::distance() precondition, which asserts on MSVC debug builds
//{
// int* p = nullptr;
// auto workaround_macro = [&]() { span<int> s{&arr[0], p}; };
// EXPECT_DEATH(workaround_macro(), expected);
//}
{ {
int* p = nullptr; int* p = nullptr;
span<int> s{p, p}; span<int> s{p, p};
@ -271,19 +275,21 @@ TEST(span_test, from_pointer_pointer_construction)
EXPECT_TRUE(s.size() == 0); EXPECT_TRUE(s.size() == 0);
EXPECT_TRUE(s.data() == nullptr); EXPECT_TRUE(s.data() == nullptr);
} }
// this will fail the std::distance() precondition, which asserts on MSVC debug builds
//{
// int* p = nullptr;
// auto workaround_macro = [&]() { span<int> s{&arr[0], p}; };
// EXPECT_DEATH(workaround_macro(), expected);
//}
} }
template <typename U, typename V, typename = void>
static constexpr bool CtorCompilesFor = false;
template <typename U, typename V>
static constexpr bool CtorCompilesFor<U, V, void_t<decltype(U{std::declval<V>()})>> = true;
TEST(span_test, from_array_constructor) TEST(span_test, from_array_constructor)
{ {
int arr[5] = {1, 2, 3, 4, 5}; int arr[5] = {1, 2, 3, 4, 5};
static_assert(!CtorCompilesFor<span<int, 6>, int[5]>, "!CtorCompilesFor<span<int, 6>, int[5]>");
static_assert(!CtorCompilesFor<span<int, 0>, int[5]>, "!CtorCompilesFor<span<int, 0>, int[5]>");
static_assert(!CtorCompilesFor<span<int>, int[2][3]>, "!CtorCompilesFor<span<int>, int[2][3]>");
{ {
const span<int> s{arr}; const span<int> s{arr};
EXPECT_TRUE(s.size() == 5); EXPECT_TRUE(s.size() == 5);
@ -298,70 +304,28 @@ TEST(span_test, from_array_constructor)
int arr2d[2][3] = {1, 2, 3, 4, 5, 6}; int arr2d[2][3] = {1, 2, 3, 4, 5, 6};
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(!CtorCompilesFor<span<int, 0>, int[2][3]>,
{ "!CtorCompilesFor<span<int, 0>, int[2][3]>");
span<int, 6> s{arr}; static_assert(!CtorCompilesFor<span<int, 6>, int[2][3]>,
} "!CtorCompilesFor<span<int, 6>, int[2][3]>");
{
span<int, 0> s{arr};
EXPECT_TRUE(s.size() == 0);
EXPECT_TRUE(s.data() == &arr[0]);
}
{
span<int> s{arr2d};
EXPECT_TRUE(s.size() == 6);
EXPECT_TRUE(s.data() == &arr2d[0][0]);
EXPECT_TRUE(s[0] == 1);
EXPECT_TRUE(s[5] == 6);
}
{
span<int, 0> s{arr2d};
EXPECT_TRUE(s.size() == 0);
EXPECT_TRUE(s.data() == &arr2d[0][0]);
}
{
span<int, 6> s{arr2d};
}
#endif
{ {
const span<int[3]> s{std::addressof(arr2d[0]), 1}; const span<int[3]> s{std::addressof(arr2d[0]), 1};
EXPECT_TRUE(s.size() == 1); EXPECT_TRUE(s.size() == 1);
EXPECT_TRUE(s.data() == std::addressof(arr2d[0])); EXPECT_TRUE(s.data() == std::addressof(arr2d[0]));
} }
int arr3d[2][3][2] = { { {1, 2}, {3, 4}, {5, 6} }, { {7, 8}, {9, 10}, {11, 12} } }; int arr3d[2][3][2] = {{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {11, 12}}};
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(!CtorCompilesFor<span<int>, int[2][3][2]>,
{ "!CtorCompilesFor<span<int>, int[2][3][2]>");
span<int> s{arr3d}; static_assert(!CtorCompilesFor<span<int, 0>, int[2][3][2]>,
EXPECT_TRUE(s.size() == 12); "!CtorCompilesFor<span<int, 0>, int[2][3][2]>");
EXPECT_TRUE(s.data() == &arr3d[0][0][0]); static_assert(!CtorCompilesFor<span<int, 11>, int[2][3][2]>,
EXPECT_TRUE(s[0] == 1); "!CtorCompilesFor<span<int, 11>, int[2][3][2]>");
EXPECT_TRUE(s[11] == 12); static_assert(!CtorCompilesFor<span<int, 12>, int[2][3][2]>,
} "!CtorCompilesFor<span<int, 12>, int[2][3][2]>");
{
span<int, 0> s{arr3d};
EXPECT_TRUE(s.size() == 0);
EXPECT_TRUE(s.data() == &arr3d[0][0][0]);
}
{
span<int, 11> s{arr3d};
}
{
span<int, 12> s{arr3d};
EXPECT_TRUE(s.size() == 12);
EXPECT_TRUE(s.data() == &arr3d[0][0][0]);
EXPECT_TRUE(s[0] == 1);
EXPECT_TRUE(s[5] == 6);
}
#endif
{ {
const span<int[3][2]> s{std::addressof(arr3d[0]), 1}; const span<int[3][2]> s{std::addressof(arr3d[0]), 1};
EXPECT_TRUE(s.size() == 1); EXPECT_TRUE(s.size() == 1);
@ -389,6 +353,13 @@ TEST(span_test, from_dynamic_array_constructor)
delete[] arr; delete[] arr;
} }
template <typename U, typename V, typename = void>
static constexpr bool ConversionCompilesFor = false;
template <typename U, typename V>
static constexpr bool
ConversionCompilesFor<U, V, void_t<decltype(std::declval<void (*)(U)>()(std::declval<V>()))>> =
true;
TEST(span_test, from_std_array_constructor) TEST(span_test, from_std_array_constructor)
{ {
std::array<int, 4> arr = {1, 2, 3, 4}; std::array<int, 4> arr = {1, 2, 3, 4};
@ -428,43 +399,31 @@ TEST(span_test, from_std_array_constructor)
EXPECT_TRUE(ao_arr.data() == fs.data()); EXPECT_TRUE(ao_arr.data() == fs.data());
} }
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(!CtorCompilesFor<span<int, 2>, std::array<int, 4>&>,
{ "!CtorCompilesFor<span<int, 2>, std::array<int, 4>&>");
span<int, 2> s{arr}; static_assert(!CtorCompilesFor<span<const int, 2>, std::array<int, 4>&>,
EXPECT_TRUE(s.size() == 2); "!CtorCompilesFor<span<const int, 2>, std::array<int, 4>&>");
EXPECT_TRUE(s.data() == arr.data());
span<const int, 2> cs{arr}; static_assert(!CtorCompilesFor<span<int, 0>, std::array<int, 4>&>,
EXPECT_TRUE(cs.size() == 2); "!CtorCompilesFor<span<int, 0>, std::array<int, 4>&>");
EXPECT_TRUE(cs.data() == arr.data()); static_assert(!CtorCompilesFor<span<const int, 0>, std::array<int, 4>&>,
} "!CtorCompilesFor<span<const int, 0>, std::array<int, 4>&>");
{ static_assert(!CtorCompilesFor<span<int, 5>, std::array<int, 4>&>,
span<int, 0> s{arr}; "!CtorCompilesFor<span<int, 5>, std::array<int, 4>&>");
EXPECT_TRUE(s.size() == 0);
EXPECT_TRUE(s.data() == arr.data());
span<const int, 0> cs{arr}; #if !defined(_MSC_VER) || (_MSC_VER > 1943) || (__cplusplus >= 201703L)
EXPECT_TRUE(cs.size() == 0); // Fails on "Visual Studio 16 2019/Visual Studio 17 2022, windows-2019/2022, Debug/Release, 14".
EXPECT_TRUE(cs.data() == arr.data()); static_assert(!ConversionCompilesFor<span<int>, std::array<int, 4>>,
} "!ConversionCompilesFor<span<int>, std::array<int, 4>>");
{
span<int, 5> s{arr};
}
{
auto get_an_array = []() -> std::array<int, 4> { return {1, 2, 3, 4}; };
auto take_a_span = [](span<int> s) { static_cast<void>(s); };
// try to take a temporary std::array
take_a_span(get_an_array());
}
#endif #endif
{ {
auto get_an_array = []() -> std::array<int, 4> { return {1, 2, 3, 4}; }; auto get_an_array = []() -> std::array<int, 4> { return {1, 2, 3, 4}; };
auto take_a_span = [](span<const int> s) { static_cast<void>(s); }; auto take_a_span = [](span<const int>) {};
// try to take a temporary std::array // try to take a temporary std::array
static_assert(ConversionCompilesFor<span<const int>, std::array<int, 4>>,
"ConversionCompilesFor<span<const int>, std::array<int, 4>>");
take_a_span(get_an_array()); take_a_span(get_an_array());
} }
} }
@ -493,23 +452,12 @@ TEST(span_test, from_const_std_array_constructor)
EXPECT_TRUE(s.data() == ao_arr.data()); EXPECT_TRUE(s.data() == ao_arr.data());
} }
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(!CtorCompilesFor<span<const int, 2>, std::array<int, 4>&>,
{ "!CtorCompilesFor<span<const int, 2>, std::array<int, 4>&>");
span<const int, 2> s{arr}; static_assert(!CtorCompilesFor<span<const int, 0>, std::array<int, 4>&>,
EXPECT_TRUE(s.size() == 2); "!CtorCompilesFor<span<const int, 0>, std::array<int, 4>&>");
EXPECT_TRUE(s.data() == arr.data()); static_assert(!CtorCompilesFor<span<int, 5>, std::array<int, 4>&>,
} "!CtorCompilesFor<span<int, 5>, std::array<int, 4>&>");
{
span<const int, 0> s{arr};
EXPECT_TRUE(s.size() == 0);
EXPECT_TRUE(s.data() == arr.data());
}
{
span<const int, 5> s{arr};
}
#endif
{ {
auto get_an_array = []() -> const std::array<int, 4> { return {1, 2, 3, 4}; }; auto get_an_array = []() -> const std::array<int, 4> { return {1, 2, 3, 4}; };
@ -535,27 +483,14 @@ TEST(span_test, from_std_array_const_constructor)
EXPECT_TRUE(s.data() == arr.data()); EXPECT_TRUE(s.data() == arr.data());
} }
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(!CtorCompilesFor<span<const int, 2>, const std::array<int, 4>&>,
{ "!CtorCompilesFor<span<const int, 2>, const std::array<int, 4>&>");
span<const int, 2> s{arr}; static_assert(!CtorCompilesFor<span<const int, 0>, const std::array<int, 4>&>,
EXPECT_TRUE(s.size() == 2); "!CtorCompilesFor<span<const int, 0>, const std::array<int, 4>&>");
EXPECT_TRUE(s.data() == arr.data()); static_assert(!CtorCompilesFor<span<const int, 5>, const std::array<int, 4>&>,
} "!CtorCompilesFor<span<const int, 5>, const std::array<int, 4>&>");
static_assert(!CtorCompilesFor<span<int, 5>, const std::array<int, 4>&>,
{ "!CtorCompilesFor<span<int, 5>, const std::array<int, 4>&>");
span<const int, 0> s{arr};
EXPECT_TRUE(s.size() == 0);
EXPECT_TRUE(s.data() == arr.data());
}
{
span<const int, 5> s{arr};
}
{
span<int, 4> s{arr};
}
#endif
} }
TEST(span_test, from_container_constructor) TEST(span_test, from_container_constructor)
@ -577,32 +512,28 @@ TEST(span_test, from_container_constructor)
const std::string cstr = "hello"; const std::string cstr = "hello";
{ {
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(CtorCompilesFor<span<char>, std::string&> == (__cplusplus >= 201703L),
span<char> s{str}; "CtorCompilesFor<span<char>, std::string&> == (__cplusplus >= 201703L)");
EXPECT_TRUE(s.size() == str.size());
EXPECT_TRUE(s.data() == str.data())); span<const char> cs{str};
#endif EXPECT_TRUE(cs.size() == str.size());
span<const char> cs{str}; EXPECT_TRUE(cs.data() == str.data());
EXPECT_TRUE(cs.size() == str.size());
EXPECT_TRUE(cs.data() == str.data());
} }
{ {
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(!CtorCompilesFor<span<char>, const std::string&>,
span<char> s{cstr}; "!CtorCompilesFor<span<char>, const std::string&>");
#endif
span<const char> cs{cstr}; span<const char> cs{cstr};
EXPECT_TRUE(cs.size() == cstr.size()); EXPECT_TRUE(cs.size() == cstr.size());
EXPECT_TRUE(cs.data() == cstr.data()); EXPECT_TRUE(cs.data() == cstr.data());
} }
{ #if !defined(_MSC_VER) || (_MSC_VER > 1943) || (__cplusplus >= 201703L)
#ifdef CONFIRM_COMPILATION_ERRORS // Fails on "Visual Studio 16 2019/Visual Studio 17 2022, windows-2019/2022, Debug/Release, 14".
auto get_temp_vector = []() -> std::vector<int> { return {}; }; static_assert(!ConversionCompilesFor<span<int>, std::vector<int>>,
auto use_span = [](span<int> s) { static_cast<void>(s); }; "!ConversionCompilesFor<span<int>, std::vector<int>>");
use_span(get_temp_vector()); #endif // !defined(_MSC_VER) || (_MSC_VER > 1942) || (__cplusplus >= 201703L)
#endif
}
{ {
auto get_temp_vector = []() -> std::vector<int> { return {}; }; auto get_temp_vector = []() -> std::vector<int> { return {}; };
@ -610,13 +541,8 @@ TEST(span_test, from_container_constructor)
use_span(get_temp_vector()); use_span(get_temp_vector());
} }
{ static_assert(!ConversionCompilesFor<span<char>, std::string>,
#ifdef CONFIRM_COMPILATION_ERRORS "!ConversionCompilesFor<span<char>, std::string>");
auto get_temp_string = []() -> std::string { return {}; };
auto use_span = [](span<char> s) { static_cast<void>(s); };
use_span(get_temp_string());
#endif
}
{ {
auto get_temp_string = []() -> std::string { return {}; }; auto get_temp_string = []() -> std::string { return {}; };
@ -624,13 +550,10 @@ TEST(span_test, from_container_constructor)
use_span(get_temp_string()); use_span(get_temp_string());
} }
{ static_assert(!ConversionCompilesFor<span<const char>, const std::vector<int>>,
#ifdef CONFIRM_COMPILATION_ERRORS "!ConversionCompilesFor<span<const char>, const std::vector<int>>");
auto get_temp_vector = []() -> const std::vector<int> { return {}; }; static_assert(!ConversionCompilesFor<span<char>, const std::string>,
auto use_span = [](span<const char> s) { static_cast<void>(s); }; "!ConversionCompilesFor<span<char>, const std::string>");
use_span(get_temp_vector());
#endif
}
{ {
auto get_temp_string = []() -> const std::string { return {}; }; auto get_temp_string = []() -> const std::string { return {}; };
@ -638,12 +561,8 @@ TEST(span_test, from_container_constructor)
use_span(get_temp_string()); use_span(get_temp_string());
} }
{ static_assert(!CtorCompilesFor<span<int>, std::map<int, int>&>,
#ifdef CONFIRM_COMPILATION_ERRORS "!CtorCompilesFor<span<int>, std::map<int, int>&>");
std::map<int, int> m;
span<int> s{m};
#endif
}
} }
TEST(span_test, from_convertible_span_constructor) TEST(span_test, from_convertible_span_constructor)
@ -695,52 +614,20 @@ TEST(span_test, from_convertible_span_constructor)
EXPECT_DEATH(T{avd}, expected); EXPECT_DEATH(T{avd}, expected);
} }
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(!ConversionCompilesFor<span<const DerivedClass, 2>, span<DerivedClass>&>,
{ "!ConversionCompilesFor<span<const DerivedClass, 2>, span<DerivedClass>&>");
std::array<DerivedClass, 2> arr{}; static_assert(!ConversionCompilesFor<span<const DerivedClass, 1>, span<DerivedClass, 2>&>,
span<DerivedClass> avd{arr}; "!ConversionCompilesFor<span<const DerivedClass, 1>, span<DerivedClass, 2>&>");
span<const DerivedClass, 2> avcd = avd; static_assert(!ConversionCompilesFor<span<const DerivedClass, 3>, span<DerivedClass, 2>&>,
static_cast<void>(avcd); "!ConversionCompilesFor<span<const DerivedClass, 3>, span<DerivedClass, 2>&>");
} static_assert(!ConversionCompilesFor<span<BaseClass, 3>, span<DerivedClass>&>,
"!ConversionCompilesFor<span<BaseClass, 3>, span<DerivedClass>&>");
{ static_assert(!ConversionCompilesFor<span<unsigned int>, span<int>&>,
std::array<DerivedClass, 2> arr{}; "!ConversionCompilesFor<span<unsigned int>, span<int>&>");
span<DerivedClass, 2> avd{arr}; static_assert(!ConversionCompilesFor<span<const unsigned int>, span<int>&>,
span<const DerivedClass, 1> avcd = avd; "!ConversionCompilesFor<span<const unsigned int>, span<int>&>");
static_cast<void>(avcd); static_assert(!ConversionCompilesFor<span<short>, span<int>&>,
} "!ConversionCompilesFor<span<short>, span<int>&>");
{
std::array<DerivedClass, 2> arr{};
span<DerivedClass, 2> avd{arr};
span<const DerivedClass, 3> avcd = avd;
static_cast<void>(avcd);
}
{
span<DerivedClass> avd;
span<BaseClass> avb = avd;
static_cast<void>(avb);
}
{
span<int> s;
span<unsigned int> s2 = s;
static_cast<void>(s2);
}
{
span<int> s;
span<const unsigned int> s2 = s;
static_cast<void>(s2);
}
{
span<int> s;
span<short> s2 = s;
static_cast<void>(s2);
}
#endif
} }
TEST(span_test, copy_move_and_assignment) TEST(span_test, copy_move_and_assignment)
@ -800,10 +687,9 @@ TEST(span_test, first)
{ {
span<int, 5> av = arr; span<int, 5> av = arr;
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
EXPECT_TRUE(av.first<6>().size() == 6); (void) av.first<6>();
EXPECT_TRUE(av.first<-1>().size() == -1);
#endif #endif
EXPECT_DEATH(av.first(6).size(), expected); EXPECT_DEATH(av.first(6), expected);
} }
{ {
@ -844,9 +730,9 @@ TEST(span_test, last)
{ {
span<int, 5> av = arr; span<int, 5> av = arr;
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
EXPECT_TRUE(av.last<6>().size() == 6); (void) av.last<6>();
#endif #endif
EXPECT_DEATH(av.last(6).size(), expected); EXPECT_DEATH(av.last(6), expected);
} }
{ {
@ -871,6 +757,9 @@ TEST(span_test, subspan)
EXPECT_TRUE((av.subspan<2, 2>().size()) == 2); EXPECT_TRUE((av.subspan<2, 2>().size()) == 2);
EXPECT_TRUE(decltype(av.subspan<2, 2>())::extent == 2); EXPECT_TRUE(decltype(av.subspan<2, 2>())::extent == 2);
EXPECT_TRUE(av.subspan(2, 2).size() == 2); EXPECT_TRUE(av.subspan(2, 2).size() == 2);
EXPECT_TRUE((av.subspan<2, 3>().size()) == 3);
EXPECT_TRUE(decltype(av.subspan<2, 3>())::extent == 3);
EXPECT_TRUE(av.subspan(2, 3).size() == 3); EXPECT_TRUE(av.subspan(2, 3).size() == 3);
} }
@ -887,8 +776,12 @@ TEST(span_test, subspan)
EXPECT_TRUE(decltype(av.subspan<0, 5>())::extent == 5); EXPECT_TRUE(decltype(av.subspan<0, 5>())::extent == 5);
EXPECT_TRUE(av.subspan(0, 5).size() == 5); EXPECT_TRUE(av.subspan(0, 5).size() == 5);
EXPECT_DEATH(av.subspan(0, 6).size(), expected); #ifdef CONFIRM_COMPILATION_ERRORS
EXPECT_DEATH(av.subspan(1, 5).size(), expected); (void) av.subspan<0, 6>();
(void) av.subspan<1, 5>();
#endif
EXPECT_DEATH(av.subspan(0, 6), expected);
EXPECT_DEATH(av.subspan(1, 5), expected);
} }
{ {
@ -896,14 +789,22 @@ TEST(span_test, subspan)
EXPECT_TRUE((av.subspan<4, 0>().size()) == 0); EXPECT_TRUE((av.subspan<4, 0>().size()) == 0);
EXPECT_TRUE(decltype(av.subspan<4, 0>())::extent == 0); EXPECT_TRUE(decltype(av.subspan<4, 0>())::extent == 0);
EXPECT_TRUE(av.subspan(4, 0).size() == 0); EXPECT_TRUE(av.subspan(4, 0).size() == 0);
EXPECT_TRUE((av.subspan<5, 0>().size()) == 0);
EXPECT_TRUE(decltype(av.subspan<5, 0>())::extent == 0);
EXPECT_TRUE(av.subspan(5, 0).size() == 0); EXPECT_TRUE(av.subspan(5, 0).size() == 0);
EXPECT_DEATH(av.subspan(6, 0).size(), expected);
#ifdef CONFIRM_COMPILATION_ERRORS
(void) av.subspan<6, 0>();
#endif
EXPECT_DEATH(av.subspan(6, 0), expected);
} }
{ {
span<int, 5> av = arr; span<int, 5> av = arr;
EXPECT_TRUE(av.subspan<1>().size() == 4); EXPECT_TRUE(av.subspan<1>().size() == 4);
EXPECT_TRUE(decltype(av.subspan<1>())::extent == 4); EXPECT_TRUE(decltype(av.subspan<1>())::extent == 4);
EXPECT_TRUE(av.subspan(1).size() == 4);
} }
{ {
@ -911,35 +812,58 @@ TEST(span_test, subspan)
EXPECT_TRUE((av.subspan<0, 0>().size()) == 0); EXPECT_TRUE((av.subspan<0, 0>().size()) == 0);
EXPECT_TRUE(decltype(av.subspan<0, 0>())::extent == 0); EXPECT_TRUE(decltype(av.subspan<0, 0>())::extent == 0);
EXPECT_TRUE(av.subspan(0, 0).size() == 0); EXPECT_TRUE(av.subspan(0, 0).size() == 0);
EXPECT_DEATH((av.subspan<1, 0>().size()), expected);
EXPECT_DEATH((av.subspan<1, 0>()), expected);
EXPECT_DEATH((av.subspan(1, 0)), expected);
} }
{ {
span<int> av; span<int> av;
EXPECT_TRUE((av.subspan<0>().size()) == 0);
EXPECT_TRUE(decltype(av.subspan<0>())::extent == dynamic_extent);
EXPECT_TRUE(av.subspan(0).size() == 0); EXPECT_TRUE(av.subspan(0).size() == 0);
EXPECT_DEATH(av.subspan(1).size(), expected);
EXPECT_DEATH(av.subspan<1>(), expected);
EXPECT_TRUE(decltype(av.subspan<1>())::extent == dynamic_extent);
EXPECT_DEATH(av.subspan(1), expected);
} }
{ {
span<int> av = arr; span<int> av = arr;
EXPECT_TRUE(av.subspan(0).size() == 5); EXPECT_TRUE(av.subspan(0).size() == 5);
EXPECT_TRUE(av.subspan<0>().size() == 5);
EXPECT_TRUE(av.subspan(1).size() == 4); EXPECT_TRUE(av.subspan(1).size() == 4);
EXPECT_TRUE(av.subspan<1>().size() == 4);
EXPECT_TRUE(av.subspan(4).size() == 1); EXPECT_TRUE(av.subspan(4).size() == 1);
EXPECT_TRUE(av.subspan<4>().size() == 1);
EXPECT_TRUE(av.subspan(5).size() == 0); EXPECT_TRUE(av.subspan(5).size() == 0);
EXPECT_DEATH(av.subspan(6).size(), expected); EXPECT_TRUE(av.subspan<5>().size() == 0);
EXPECT_DEATH(av.subspan(6), expected);
EXPECT_DEATH(av.subspan<6>(), expected);
const auto av2 = av.subspan(1); const auto av2 = av.subspan(1);
for (std::size_t i = 0; i < 4; ++i) EXPECT_TRUE(av2[i] == static_cast<int>(i) + 2); for (std::size_t i = 0; i < 4; ++i) EXPECT_TRUE(av2[i] == static_cast<int>(i) + 2);
const auto av3 = av.subspan<1>();
for (std::size_t i = 0; i < 4; ++i) EXPECT_TRUE(av3[i] == static_cast<int>(i) + 2);
} }
{ {
span<int, 5> av = arr; span<int, 5> av = arr;
EXPECT_TRUE(av.subspan(0).size() == 5); EXPECT_TRUE(av.subspan(0).size() == 5);
EXPECT_TRUE(av.subspan<0>().size() == 5);
EXPECT_TRUE(av.subspan(1).size() == 4); EXPECT_TRUE(av.subspan(1).size() == 4);
EXPECT_TRUE(av.subspan<1>().size() == 4);
EXPECT_TRUE(av.subspan(4).size() == 1); EXPECT_TRUE(av.subspan(4).size() == 1);
EXPECT_TRUE(av.subspan<4>().size() == 1);
EXPECT_TRUE(av.subspan(5).size() == 0); EXPECT_TRUE(av.subspan(5).size() == 0);
EXPECT_DEATH(av.subspan(6).size(), expected); EXPECT_TRUE(av.subspan<5>().size() == 0);
EXPECT_DEATH(av.subspan(6), expected);
#ifdef CONFIRM_COMPILATION_ERRORS
EXPECT_DEATH(av.subspan<6>(), expected);
#endif
const auto av2 = av.subspan(1); const auto av2 = av.subspan(1);
for (std::size_t i = 0; i < 4; ++i) EXPECT_TRUE(av2[i] == static_cast<int>(i) + 2); for (std::size_t i = 0; i < 4; ++i) EXPECT_TRUE(av2[i] == static_cast<int>(i) + 2);
const auto av3 = av.subspan<1>();
for (std::size_t i = 0; i < 4; ++i) EXPECT_TRUE(av3[i] == static_cast<int>(i) + 2);
} }
} }
@ -1114,9 +1038,21 @@ TEST(span_test, rbegin_rend)
} }
} }
template <typename U, typename = void>
static constexpr bool AsWritableBytesCompilesFor = false;
template <typename U>
static constexpr bool
AsWritableBytesCompilesFor<U, void_t<decltype(as_writable_bytes(std::declval<U>()))>> = true;
TEST(span_test, as_bytes) TEST(span_test, as_bytes)
{ {
int a[] = {1, 2, 3, 4}; int a[] = {1, 2, 3, 4};
static_assert(AsWritableBytesCompilesFor<span<int>>, "AsWriteableBytesCompilesFor<span<int>>");
// you should not be able to get writeable bytes for const objects
static_assert(!AsWritableBytesCompilesFor<span<const int>>,
"!AsWriteableBytesCompilesFor<span<const int>>");
{ {
const span<const int> s = a; const span<const int> s = a;
EXPECT_TRUE(s.size() == 4); EXPECT_TRUE(s.size() == 4);
@ -1147,17 +1083,6 @@ TEST(span_test, as_writable_bytes)
{ {
int a[] = {1, 2, 3, 4}; int a[] = {1, 2, 3, 4};
{
#ifdef CONFIRM_COMPILATION_ERRORS
// you should not be able to get writeable bytes for const objects
span<const int> s = a;
EXPECT_TRUE(s.size() == 4);
span<const byte> bs = as_writable_bytes(s);
EXPECT_TRUE(static_cast<void*>(bs.data()) == static_cast<void*>(s.data()));
EXPECT_TRUE(bs.size() == s.size_bytes());
#endif
}
{ {
span<int> s; span<int> s;
const auto bs = as_writable_bytes(s); const auto bs = as_writable_bytes(s);
@ -1197,30 +1122,15 @@ TEST(span_test, fixed_size_conversions)
static_cast<void>(s); static_cast<void>(s);
} }
// initialization or assignment to static span that REDUCES size is NOT ok // initialization or assignment to static span that REDUCES size is NOT ok
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(!ConversionCompilesFor<span<int, 2>, int[4]>,
{ "!ConversionCompilesFor<span<int, 2>, int[4]>");
span<int, 2> s = arr; static_assert(!ConversionCompilesFor<span<int, 2>, span<int, 4>>,
} "!ConversionCompilesFor<span<int, 2>, span<int, 4>>");
{
span<int, 2> s2 = s4;
static_cast<void>(s2);
}
#endif
// even when done dynamically // even when done dynamically
{ static_assert(!ConversionCompilesFor<span<int, 2>, span<int>>,
/* "!ConversionCompilesFor<span<int, 2>, span<int>>");
// this now results in a compile-time error, rather than runtime.
// There is no suitable conversion from dynamic span to fixed span.
span<int> s = arr;
auto f = [&]() {
const span<int, 2> s2 = s;
static_cast<void>(s2);
};
EXPECT_DEATH(f(), expected);
*/
}
// but doing so explicitly is ok // but doing so explicitly is ok
@ -1234,32 +1144,24 @@ TEST(span_test, fixed_size_conversions)
static_cast<void>(s1); static_cast<void>(s1);
} }
/* // this is not a legal operation in std::span, so we are no longer supporting it
// this is not a legal operation in std::span, so we are no longer supporting it // conversion from span<int, 4> to span<int, dynamic_extent> via call to `first`
// conversion from span<int, 4> to span<int, dynamic_extent> via call to `first` // then convert from span<int, dynamic_extent> to span<int, 1>
// then convert from span<int, dynamic_extent> to span<int, 1> // The dynamic to fixed extents are not supported in the standard
// The dynamic to fixed extents are not supported in the standard // to make this work, span<int, 1> would need to be span<int>.
// to make this work, span<int, 1> would need to be span<int>. static_assert(!ConversionCompilesFor<span<int, 1>, span<int>>,
{ "!ConversionCompilesFor<span<int, 1>, span<int>>");
// NB: implicit conversion to span<int,1> from span<int>
span<int, 1> s1 = s4.first(1);
static_cast<void>(s1);
}
*/
// initialization or assignment to static span that requires size INCREASE is not ok. // initialization or assignment to static span that requires size INCREASE is not ok.
int arr2[2] = {1, 2}; int arr2[2] = {1, 2};
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(!ConversionCompilesFor<span<int, 4>, int[2]>,
{ "!ConversionCompilesFor<span<int, 4>, int[2]>");
span<int, 4> s3 = arr2; static_assert(!ConversionCompilesFor<span<int, 2>, int[2]>,
} "!ConversionCompilesFor<span<int, 2>, int[2]>");
{ static_assert(!ConversionCompilesFor<span<int, 4>, span<int, 2>>,
span<int, 2> s2 = arr2; "!ConversionCompilesFor<span<int, 4>, span<int, 2>>");
span<int, 4> s4a = s2;
}
#endif
{ {
auto f = [&]() { auto f = [&]() {
const span<int, 4> _s4{arr2, 2}; const span<int, 4> _s4{arr2, 2};
@ -1268,16 +1170,11 @@ TEST(span_test, fixed_size_conversions)
EXPECT_DEATH(f(), expected); EXPECT_DEATH(f(), expected);
} }
/* // This no longer compiles. There is no suitable conversion from dynamic span to a fixed size
// This no longer compiles. There is no suitable conversion from dynamic span to a fixed size // span.
span. // this should fail - we are trying to assign a small dynamic span to a fixed_size larger one
// this should fail - we are trying to assign a small dynamic span to a fixed_size larger one static_assert(!ConversionCompilesFor<span<int, 4>, span<int>>,
span<int> av = arr2; auto f = [&]() { "!ConversionCompilesFor<span<int, 4>, span<int>>");
const span<int, 4> _s4 = av;
static_cast<void>(_s4);
};
EXPECT_DEATH(f(), expected);
*/
} }
TEST(span_test, interop_with_std_regex) TEST(span_test, interop_with_std_regex)
@ -1376,8 +1273,154 @@ TEST(span_test, msvc_compile_error_PR1100)
int arr[]{1, 7, 2, 9}; int arr[]{1, 7, 2, 9};
gsl::span sp{arr, std::size(arr)}; gsl::span sp{arr, std::size(arr)};
std::ranges::sort(sp); std::ranges::sort(sp);
for (const auto& e : sp) { for (const auto& e : sp) { (void) e; }
(void)e;
}
} }
#endif // defined(__cpp_lib_span) && defined(__cpp_lib_ranges) #endif // defined(__cpp_lib_span) && defined(__cpp_lib_ranges)
TEST(span_test, empty_span)
{
span<int> s{};
EXPECT_TRUE(s.empty());
EXPECT_TRUE(s.size() == 0);
EXPECT_TRUE(s.data() == nullptr);
span<const int> cs{};
EXPECT_TRUE(cs.empty());
EXPECT_TRUE(cs.size() == 0);
EXPECT_TRUE(cs.data() == nullptr);
}
TEST(span_test, conversions)
{
int arr[5] = {1, 2, 3, 4, 5};
#if defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L)
span s = arr;
span cs = s;
#else
span<int, 5> s = arr;
span<int, 5> cs = s;
#endif
EXPECT_TRUE(cs.size() == s.size());
EXPECT_TRUE(cs.data() == s.data());
span<int, 5> fs = s;
EXPECT_TRUE(fs.size() == s.size());
EXPECT_TRUE(fs.data() == s.data());
span<const int, 5> cfs = s;
EXPECT_TRUE(cfs.size() == s.size());
EXPECT_TRUE(cfs.data() == s.data());
}
TEST(span_test, comparison_operators)
{
int arr1[3] = {1, 2, 3};
int arr2[3] = {1, 2, 3};
int arr3[3] = {4, 5, 6};
span<int> s1 = arr1;
span<int> s2 = arr2;
span<int> s3 = arr3;
EXPECT_TRUE(s1 == s2);
EXPECT_FALSE(s1 != s2);
EXPECT_FALSE(s1 == s3);
EXPECT_TRUE(s1 != s3);
EXPECT_TRUE(s1 < s3);
EXPECT_FALSE(s3 < s1);
EXPECT_TRUE(s1 <= s2);
EXPECT_TRUE(s1 <= s3);
EXPECT_FALSE(s3 <= s1);
EXPECT_TRUE(s3 > s1);
EXPECT_FALSE(s1 > s3);
EXPECT_TRUE(s3 >= s1);
EXPECT_TRUE(s1 >= s2);
EXPECT_FALSE(s1 >= s3);
}
// ...existing code...
#if defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
#include <span> // for std::span
TEST(span_test, compare_empty_span)
{
gsl::span<int> gsl_s{};
std::span<int> std_s{};
EXPECT_TRUE(gsl_s.empty());
EXPECT_TRUE(std_s.empty());
EXPECT_EQ(gsl_s.size(), std_s.size());
EXPECT_EQ(gsl_s.data(), std_s.data());
}
TEST(span_test, compare_subspan)
{
int arr[5] = {1, 2, 3, 4, 5};
gsl::span gsl_s = arr;
std::span std_s = arr;
auto gsl_sub1 = gsl_s.subspan(1);
auto std_sub1 = std_s.subspan(1);
EXPECT_EQ(gsl_sub1.size(), std_sub1.size());
EXPECT_EQ(gsl_sub1.data(), std_sub1.data());
auto gsl_sub2 = gsl_s.subspan(1, 2);
auto std_sub2 = std_s.subspan(1, 2);
EXPECT_EQ(gsl_sub2.size(), std_sub2.size());
EXPECT_EQ(gsl_sub2.data(), std_sub2.data());
}
TEST(span_test, compare_conversions)
{
int arr[5] = {1, 2, 3, 4, 5};
gsl::span gsl_s = arr;
std::span std_s = arr;
gsl::span gsl_cs = gsl_s;
std::span std_cs = std_s;
EXPECT_EQ(gsl_cs.size(), std_cs.size());
EXPECT_EQ(gsl_cs.data(), std_cs.data());
gsl::span<int, 5> gsl_fs = gsl_s;
std::span<int, 5> std_fs = std_s;
EXPECT_EQ(gsl_fs.size(), std_fs.size());
EXPECT_EQ(gsl_fs.data(), std_fs.data());
gsl::span<const int, 5> gsl_cfs = gsl_s;
std::span<const int, 5> std_cfs = std_s;
EXPECT_EQ(gsl_cfs.size(), std_cfs.size());
EXPECT_EQ(gsl_cfs.data(), std_cfs.data());
}
TEST(span_test, deduction_guides)
{
int arr[5] = {1, 2, 3, 4, 5};
std::array<int, 5> std_arr = {1, 2, 3, 4, 5};
std::vector<int> vec = {1, 2, 3, 4, 5};
// Test deduction guides for gsl::span
gsl::span gsl_s1 = arr;
gsl::span gsl_s2 = std_arr;
gsl::span gsl_s3 = vec;
// Test deduction guides for std::span (for sanity checks)
std::span std_s1 = arr;
std::span std_s2 = std_arr;
std::span std_s3 = vec;
// Compare sizes
EXPECT_EQ(gsl_s1.size(), std_s1.size());
EXPECT_EQ(gsl_s2.size(), std_s2.size());
EXPECT_EQ(gsl_s3.size(), std_s3.size());
// Compare data pointers
EXPECT_EQ(gsl_s1.data(), std_s1.data());
EXPECT_EQ(gsl_s2.data(), std_s2.data());
EXPECT_EQ(gsl_s3.data(), std_s3.data());
}
#endif // defined(__cpp_lib_span) && __cpp_lib_span >= 202002L

View File

@ -17,10 +17,28 @@
#include <gsl/pointers> // for not_null, operator<, operator<=, operator> #include <gsl/pointers> // for not_null, operator<, operator<=, operator>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <type_traits> // for declval
#include "deathTestCommon.h" #include "deathTestCommon.h"
using namespace gsl; using namespace gsl;
#if __cplusplus >= 201703l
using std::void_t;
#else // __cplusplus >= 201703l
template <class...>
using void_t = void;
#endif // __cplusplus < 201703l
// stand-in for a user-defined ref-counted class
template <typename T>
struct RefCounted
{
RefCounted(T* p) : p_(p) {}
operator T*() { return p_; }
T* p_;
};
namespace namespace
{ {
// clang-format off // clang-format off
@ -43,12 +61,154 @@ GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
// clang-format on // clang-format on
bool strict_helper_const(strict_not_null<const int*> p) { return *p == 12; } bool strict_helper_const(strict_not_null<const int*> p) { return *p == 12; }
#ifdef CONFIRM_COMPILATION_ERRORS
int* return_pointer() { return nullptr; } int* return_pointer() { return nullptr; }
const int* return_pointer_const() { return nullptr; }
#endif
} // namespace } // namespace
template <typename U, typename = void>
static constexpr bool CtorCompilesFor_A = false;
template <typename U>
static constexpr bool
CtorCompilesFor_A<U, void_t<decltype(gsl::strict_not_null<void*>{std::declval<U>()})>> = true;
template <typename U, int N, typename = void>
static constexpr bool CtorCompilesFor_B = false;
template <typename U, int N>
static constexpr bool CtorCompilesFor_B<U, N, void_t<decltype(gsl::strict_not_null<U>{N})>> = true;
template <typename U, typename = void>
static constexpr bool DefaultCtorCompilesFor = false;
template <typename U>
static constexpr bool DefaultCtorCompilesFor<U, void_t<decltype(gsl::strict_not_null<U>{})>> = true;
template <typename U, typename = void>
static constexpr bool CtorCompilesFor_C = false;
template <typename U>
static constexpr bool CtorCompilesFor_C<
U, void_t<decltype(gsl::strict_not_null<U*>{std::declval<std::unique_ptr<U>>()})>> = true;
TEST(strict_notnull_tests, TestStrictNotNullConstructors)
{
{
static_assert(CtorCompilesFor_A<void*>, "CtorCompilesFor_A<void*>");
static_assert(!CtorCompilesFor_A<std::nullptr_t>, "!CtorCompilesFor_A<std::nullptr_t>");
static_assert(!CtorCompilesFor_B<void*, 0>, "!CtorCompilesFor_B<void*, 0>");
static_assert(!DefaultCtorCompilesFor<void*>, "!DefaultCtorCompilesFor<void*>");
static_assert(!CtorCompilesFor_C<int>, "CtorCompilesFor_C<int>");
#ifdef CONFIRM_COMPILATION_ERRORS
// Forbid non-nullptr assignable types
strict_not_null<std::vector<int>> f(std::vector<int>{1});
strict_not_null<int> z(10);
strict_not_null<std::vector<int>> y({1, 2});
#endif
}
const auto terminateHandler = std::set_terminate([] {
std::cerr << "Expected Death. TestNotNullConstructors";
std::abort();
});
const auto expected = GetExpectedDeathString(terminateHandler);
{
// from shared pointer
int i = 12;
auto rp = RefCounted<int>(&i);
strict_not_null<int*> p(rp);
EXPECT_TRUE(p.get() == &i);
strict_not_null<std::shared_ptr<int>> x(
std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
int* pi = nullptr;
EXPECT_DEATH((strict_not_null<decltype(pi)>(pi)), expected);
}
{
// from unique pointer
strict_not_null<std::unique_ptr<int>> x(
std::make_unique<int>(10)); // unique_ptr<int> is nullptr assignable
EXPECT_DEATH((strict_not_null<std::unique_ptr<int>>(std::unique_ptr<int>{})), expected);
}
{
// from pointer to local
int t = 42;
strict_not_null<int*> x{&t};
helper(&t);
helper_const(&t);
EXPECT_TRUE(*x == 42);
}
{
// from raw pointer
// from strict_not_null pointer
int t = 42;
int* p = &t;
strict_not_null<int*> x{p};
helper(p);
helper_const(p);
helper(x);
helper_const(x);
EXPECT_TRUE(*x == 42);
}
{
// from raw const pointer
// from strict_not_null const pointer
int t = 42;
const int* cp = &t;
strict_not_null<const int*> x{cp};
helper_const(cp);
helper_const(x);
EXPECT_TRUE(*x == 42);
}
{
// from strict_not_null const pointer, using auto
int t = 42;
const int* cp = &t;
auto x = strict_not_null<const int*>{cp};
EXPECT_TRUE(*x == 42);
}
{
// from returned pointer
EXPECT_DEATH(helper(return_pointer()), expected);
EXPECT_DEATH(helper_const(return_pointer()), expected);
}
}
template <typename U, typename = void>
static constexpr bool StrictHelperCompilesFor = false;
template <typename U>
static constexpr bool
StrictHelperCompilesFor<U, void_t<decltype(strict_helper(std::declval<U>()))>> = true;
template <typename U, typename = void>
static constexpr bool StrictHelperConstCompilesFor = false;
template <typename U>
static constexpr bool
StrictHelperConstCompilesFor<U, void_t<decltype(strict_helper_const(std::declval<U>()))>> =
true;
template <typename U, typename = void>
static constexpr bool HelperCompilesFor = false;
template <typename U>
static constexpr bool HelperCompilesFor<U, void_t<decltype(helper(std::declval<U>()))>> = true;
TEST(strict_notnull_tests, TestStrictNotNull) TEST(strict_notnull_tests, TestStrictNotNull)
{ {
{ {
@ -57,14 +217,15 @@ TEST(strict_notnull_tests, TestStrictNotNull)
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
strict_not_null<int*> snn = &x; strict_not_null<int*> snn = &x;
strict_helper(&x);
strict_helper_const(&x);
strict_helper(return_pointer());
strict_helper_const(return_pointer_const());
#endif #endif
static_assert(!StrictHelperCompilesFor<int*>, "!StrictHelperCompilesFor<int*>");
static_assert(!StrictHelperConstCompilesFor<int*>,
"!StrictHelperCompilesFor<int*>");
const strict_not_null<int*> snn1{&x}; const strict_not_null<int*> snn1{&x};
static_assert(StrictHelperCompilesFor<const strict_not_null<int*>>,
"StrictHelperCompilesFor<const strict_not_null<int*>>");
helper(snn1); helper(snn1);
helper_const(snn1); helper_const(snn1);
@ -77,17 +238,17 @@ TEST(strict_notnull_tests, TestStrictNotNull)
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
strict_not_null<int*> snn = &x; strict_not_null<int*> snn = &x;
strict_helper(&x);
strict_helper_const(&x);
strict_helper(return_pointer());
strict_helper_const(return_pointer_const());
#endif #endif
static_assert(!StrictHelperCompilesFor<const int*>, "!StrictHelperFor<const int*>");
static_assert(!StrictHelperConstCompilesFor<const int*>,
"!StrictHelperCompilesFor<const int*>");
const strict_not_null<const int*> snn1{&x}; const strict_not_null<const int*> snn1{&x};
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(!HelperCompilesFor<const strict_not_null<const int*>>,
helper(snn1); "!HelperCompilesFor<const strict_not_null<const int*>>");
#endif static_assert(StrictHelperConstCompilesFor<const strict_not_null<const int*>>,
"StrictHelperCompilesFor<const strict_not_null<const int*>>");
helper_const(snn1); helper_const(snn1);
EXPECT_TRUE(*snn1 == 42); EXPECT_TRUE(*snn1 == 42);
@ -114,9 +275,8 @@ TEST(strict_notnull_tests, TestStrictNotNull)
strict_not_null<const int*> snn1{&x}; strict_not_null<const int*> snn1{&x};
const strict_not_null<const int*> snn2{&x}; const strict_not_null<const int*> snn2{&x};
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(!StrictHelperCompilesFor<strict_not_null<const int*>>,
strict_helper(snn1); "!StrictHelperCompilesFor<strict_not_null<const int*>>");
#endif
strict_helper_const(snn1); strict_helper_const(snn1);
strict_helper_const(snn2); strict_helper_const(snn2);
@ -148,9 +308,8 @@ TEST(strict_notnull_tests, TestStrictNotNull)
const not_null<const int*> nn1 = snn; const not_null<const int*> nn1 = snn;
const not_null<const int*> nn2{snn}; const not_null<const int*> nn2{snn};
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(!HelperCompilesFor<strict_not_null<const int*>>,
helper(snn); "!HelperCompilesFor<strict_not_null<const int*>>");
#endif
helper_const(snn); helper_const(snn);
EXPECT_TRUE(snn == nn1); EXPECT_TRUE(snn == nn1);
@ -190,9 +349,8 @@ TEST(strict_notnull_tests, TestStrictNotNull)
const strict_not_null<const int*> snn1{nn}; const strict_not_null<const int*> snn1{nn};
const strict_not_null<const int*> snn2{nn}; const strict_not_null<const int*> snn2{nn};
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(!StrictHelperCompilesFor<not_null<const int*>>,
strict_helper(nn); "!StrictHelperCompilesFor<not_null<const int*>>");
#endif
strict_helper_const(nn); strict_helper_const(nn);
EXPECT_TRUE(snn1 == nn); EXPECT_TRUE(snn1 == nn);
@ -206,12 +364,13 @@ TEST(strict_notnull_tests, TestStrictNotNull)
EXPECT_TRUE(hash_nn(snn1) == hash_nn(snn2)); EXPECT_TRUE(hash_nn(snn1) == hash_nn(snn2));
EXPECT_TRUE(hash_snn(snn1) == hash_snn(nn)); EXPECT_TRUE(hash_snn(snn1) == hash_snn(nn));
} }
}
#ifdef CONFIRM_COMPILATION_ERRORS TEST(pointers_test, member_types)
{ {
strict_not_null<int*> p{nullptr}; // make sure `element_type` is inherited from `gsl::not_null`
} static_assert(std::is_same<gsl::strict_not_null<int*>::element_type, int*>::value,
#endif "check member type: element_type");
} }
#if defined(__cplusplus) && (__cplusplus >= 201703L) #if defined(__cplusplus) && (__cplusplus >= 201703L)
@ -238,9 +397,8 @@ TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction)
const int i = 42; const int i = 42;
strict_not_null x{&i}; strict_not_null x{&i};
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(!HelperCompilesFor<strict_not_null<const int*>>,
helper(strict_not_null{&i}); "!HelperCompilesFor<strict_not_null<const int*>>");
#endif
helper_const(strict_not_null{&i}); helper_const(strict_not_null{&i});
EXPECT_TRUE(*x == 42); EXPECT_TRUE(*x == 42);
@ -262,9 +420,8 @@ TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction)
const int* p = &i; const int* p = &i;
strict_not_null x{p}; strict_not_null x{p};
#ifdef CONFIRM_COMPILATION_ERRORS static_assert(!HelperCompilesFor<strict_not_null<const int*>>,
helper(strict_not_null{p}); "!HelperCompilesFor<strict_not_null<const int*>>");
#endif
helper_const(strict_not_null{p}); helper_const(strict_not_null{p});
EXPECT_TRUE(*x == 42); EXPECT_TRUE(*x == 42);