/README.md

https://github.com/martinmoene/gsl-lite · Markdown · 1183 lines · 945 code · 238 blank · 0 comment · 0 complexity · ef3cf7eab8ffd6389a24b00f72e5f084 MD5 · raw file

Large files are truncated click here to view the full file

  1. # *gsl-lite*: Guidelines Support Library for C++98, C++11 up
  2. | metadata | build | packages | try online |
  3. | -------- | ------ | -------- | ---------- |
  4. | [![Language](https://img.shields.io/badge/C%2B%2B-98/11/14/17-blue.svg)](https://en.wikipedia.org/wiki/C%2B%2B#Standardization) <br> [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT) <br> [![Version](https://badge.fury.io/gh/gsl-lite%2Fgsl-lite.svg)](https://github.com/gsl-lite/gsl-lite/releases) | [![Azure Pipelines build status](https://dev.azure.com/gsl-lite/gsl-lite/_apis/build/status/gsl-lite.gsl-lite?branchName=master)](https://dev.azure.com/gsl-lite/gsl-lite/_build/latest?definitionId=1&branchName=master) <br> [![Travis build status](https://travis-ci.com/gsl-lite/gsl-lite.svg?branch=master)](https://travis-ci.com/gsl-lite/gsl-lite) <br> [![AppVeyor build status](https://ci.appveyor.com/api/projects/status/1v6eqy68m8g7tm06?svg=true)](https://ci.appveyor.com/project/gsl-lite/gsl-lite) | [![Vcpkg](https://img.shields.io/badge/on-Vcpkg-blue.svg)](https://github.com/microsoft/vcpkg/tree/master/ports/gsl-lite) <br> [![Conan](https://img.shields.io/badge/on-conan-blue.svg)](https://bintray.com/conan/conan-center/gsl-lite%3A_/_latestVersion) <br> [![single header](https://img.shields.io/badge/latest-single%20header%20file-blue.svg)](https://raw.githubusercontent.com/gsl-lite/gsl-lite/master/include/gsl/gsl-lite.hpp) | [![Try it on Compiler Explorer](https://img.shields.io/badge/on-godbolt-blue.svg)](https://gcc.godbolt.org/z/JVtM2c) <br> [![Try it on Wandbox](https://img.shields.io/badge/on-wandbox-blue.svg)](https://wandbox.org/permlink/p9YnfiUTOYEQx0QL) |
  5. *gsl-lite* is an implementation of the [C++ Core Guidelines Support Library](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-gsl) originally based on [Microsoft GSL](https://github.com/microsoft/gsl).
  6. **Contents**
  7. - [Example usage](#example-usage)
  8. - [In a nutshell](#in-a-nutshell)
  9. - [License](#license)
  10. - [Dependencies](#dependencies)
  11. - [Installation and use](#installation-and-use)
  12. - [Version semantics](#version-semantics)
  13. - [Using *gsl-lite* in libraries](#using-gsl-lite-in-libraries)
  14. - [Configuration options](#configuration-options)
  15. - [Features](#features)
  16. - [Deprecation](#deprecation)
  17. - [Reported to work with](#reported-to-work-with)
  18. - [Building the tests](#building-the-tests)
  19. - [Other GSL implementations](#other-gsl-implementations)
  20. - [Notes and references](#notes-and-references)
  21. - [Appendix](#appendix)
  22. Example usage
  23. -------------
  24. ```Cpp
  25. #include <gsl/gsl-lite.hpp>
  26. int * use( gsl::not_null<int *> p )
  27. {
  28. // use p knowing it's not nullptr, NULL or 0.
  29. return p;
  30. }
  31. struct Widget
  32. {
  33. Widget() : owned_ptr_( new int(42) ) {}
  34. ~Widget() { delete owned_ptr_; }
  35. void work() { non_owned_ptr_ = use( owned_ptr_ ); }
  36. gsl::owner<int *> owned_ptr_; // if alias template support
  37. int * non_owned_ptr_;
  38. };
  39. int main()
  40. {
  41. Widget w;
  42. w.work();
  43. }
  44. ```
  45. ### Compile and run
  46. ```
  47. prompt> g++ -std=c++03 -Wall -I../include -o 01-basic.exe 01-basic.cpp && 01-basic.exe
  48. ```
  49. In a nutshell
  50. -------------
  51. **gsl-lite** is a single-file header-only implementation of the [C++ Core Guidelines Support Library](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-gsl) originally based on [Microsoft GSL](https://github.com/microsoft/gsl) and adapted for C++98, C++03. It also works when compiled as C++11, C++14, C++17, C++20.
  52. 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/). The library includes types like `owner<>`, `not_null<>`, `span<>`, `string_span` and [others](#features).
  53. *gsl-lite* recognizes when it is compiled for the CUDA platform and decorates some functions with `__host__` and `__device__`. See also section [API macro](#api-macro).
  54. License
  55. -------
  56. *gsl-lite* uses the [MIT](LICENSE) license.
  57. Dependencies
  58. ------------
  59. *gsl-lite* has no other dependencies than the [C++ standard library](http://en.cppreference.com/w/cpp/header).
  60. Installation and use
  61. --------------------
  62. ### As CMake package
  63. The recommended way to consume *gsl-lite* in your CMake project is to use `find_package()` and `target_link_libraries()`:
  64. ```CMake
  65. cmake_minimum_required( VERSION 3.15 FATAL_ERROR )
  66. find_package( gsl-lite 0.37 REQUIRED )
  67. project( my-program LANGUAGES CXX )
  68. add_executable( my-program main.cpp )
  69. target_link_libraries( my-program PRIVATE gsl::gsl-lite )
  70. ```
  71. There are various ways to make the `gsl-lite` package available to your project:
  72. <details>
  73. <summary>Using Vcpkg</summary>
  74. <p>
  75. 1. For the [Vcpkg package manager](https://github.com/microsoft/vcpkg/), simply run Vcpkg's install command:
  76. vcpkg install gsl-lite
  77. 2. Now, configure your project passing the Vcpkg toolchain file as a parameter:
  78. cd <my-program-source-dir>
  79. mkdir build
  80. cd build
  81. cmake -DCMAKE_TOOLCHAIN_FILE=<vcpkg-root>/scripts/buildsystems/vcpkg.cmake ..
  82. cmake --build ../build
  83. </p></details>
  84. <details>
  85. <summary>Using an exported build directory</summary>
  86. <p>
  87. 1. Clone the *gsl-lite* repository and configure a build directory with CMake:
  88. git clone git@github.com:gsl-lite/gsl-lite.git <gsl-lite-source-dir>
  89. cd <gsl-lite-source-dir>
  90. mkdir build
  91. cd build
  92. cmake ..
  93. 2. Now, configure your project passing the CMake build directory as a parameter:
  94. cd <my-program-source-dir>
  95. mkdir build
  96. cd build
  97. cmake -Dgsl-lite_DIR:PATH=<gsl-lite-source-dir>/build ..
  98. cmake --build ../build
  99. See [example/cmake-pkg/Readme.md](example/cmake-pkg/Readme.md) for a complete example.
  100. </p></details>
  101. <details>
  102. <summary>Using Conan</summary>
  103. <p>
  104. For the [Conan package manager](https://www.conan.io/), follow these steps:
  105. 1. Add Conan Center to the conan remotes:
  106. conan remote add center https://api.bintray.com/conan/conan/conan-center
  107. 2. Add a reference to *gsl-lite* to the *requires* section of your project's `conanfile.txt` file:
  108. [requires]
  109. gsl-lite/0.37@center/stable
  110. 3. Run conan's install command:
  111. conan install gsl-lite
  112. Now *gsl-lite* can be consumed as a Conan package. (TODO: elaborate!)
  113. </p></details>
  114. <details>
  115. <summary>Using Conda</summary>
  116. <p>
  117. 1. For the [conda package manager](https://conda.io), first use **one of these options** to install `gsl-lite` from the [`conda-forge`](https://conda-forge.org/) channel:
  118. * Install it in the current environment:
  119. conda install -c conda-forge gsl-lite
  120. * Install it in a different environment (named `env_name` in this example):
  121. conda install -n env_name -c conda-forge gsl-lite
  122. * Create a new environment containing *gsl-lite* (and possibly other packages, appended at the end of the command):
  123. conda create -n env_name -c conda-forge gsl-lite cmake
  124. * Add it to an already existing [`environment.yml`](https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#creating-an-environment-from-an-environment-yml-file) file, and update the environment using:
  125. conda env update
  126. 2. Then activate the environment using `conda activate env_name` (if not already activated) and proceed using the instructions from step 2 of ["As CMake package"](#as-cmake-package). Note that it's also useful to have the `cmake` package in the same environment, and explicitly passing `-DCMAKE_INSTALL_PREFIX` is not necessary.
  127. </p></details>
  128. ### Other options
  129. <details>
  130. <summary>As external Git project</summary>
  131. <p>
  132. TODO: this section needs updating
  133. Another approach is to automatically fetch the entire *gsl-lite* repository from GitHub and configure it as an external project.
  134. ```CMake
  135. cmake_minimum_required( VERSION 3.15 FATAL_ERROR )
  136. project( use-gsl-lite LANGUAGES CXX )
  137. # Set default ExternalProject root directory and add gsl-lite:
  138. set( GSL_LITE_URL https://github.com/gsl-lite/gsl-lite.git )
  139. include( ExternalProject )
  140. find_package( Git REQUIRED )
  141. set_directory_properties( PROPERTIES EP_PREFIX ${CMAKE_BINARY_DIR}/3rd_party )
  142. ExternalProject_Add(
  143. gsl-extern
  144. GIT_REPOSITORY ${GSL_LITE_URL}
  145. TIMEOUT 10
  146. UPDATE_COMMAND ${GIT_EXECUTABLE} pull
  147. CONFIGURE_COMMAND ""
  148. BUILD_COMMAND ""
  149. INSTALL_COMMAND ""
  150. LOG_DOWNLOAD ON
  151. )
  152. # Provide #include access to gsl-lite as <gsl/gsl-lite.hpp>:
  153. ExternalProject_Get_Property( gsl-extern SOURCE_DIR )
  154. set( GSL_LITE_INCLUDE_DIR ${SOURCE_DIR}/include CACHE INTERNAL "Include folder for gsl-lite" )
  155. add_library( gsl INTERFACE )
  156. target_include_directories( gsl INTERFACE ${GSL_LITE_INCLUDE_DIR} )
  157. # Build program from src:
  158. add_subdirectory( src )
  159. ```
  160. In folder src:
  161. ```CMake
  162. cmake_minimum_required( VERSION 3.15 FATAL_ERROR )
  163. project( program-using-gsl-lite LANGUAGES CXX )
  164. # Make program executable:
  165. add_executable( program main.cpp )
  166. target_link_libraries( program PRIVATE gsl::gsl-lite )
  167. ```
  168. This setup brings in more than you need, but also makes it easy to update *gsl-lite* to the latest version. See [example/cmake-extern](example/cmake-extern) for a complete example.
  169. </p></details>
  170. <details>
  171. <summary>As copied header</summary>
  172. <p>
  173. Put a copy of [`gsl-lite.hpp`](include/gsl/gsl-lite.hpp) located in folder [include/gsl](include/gsl) directly into the project source tree or somewhere reachable from your project, for example in *project-root*/include/gsl. A minimal CMake setup using this header might look as follows.
  174. In project root folder:
  175. ```CMake
  176. cmake_minimum_required( VERSION 3.15 FATAL_ERROR )
  177. project( use-gsl-lite LANGUAGES CXX )
  178. # Provide #include access to gsl-lite as 'gsl/gsl-lite.hpp':
  179. add_library( gsl-lite INTERFACE )
  180. target_include_directories( gsl-lite INTERFACE include ) # adapt as necessary
  181. # Build program from src:
  182. add_subdirectory( src )
  183. ```
  184. In folder src:
  185. ```CMake
  186. cmake_minimum_required( VERSION 3.15 FATAL_ERROR )
  187. project( program-using-gsl-lite LANGUAGES CXX )
  188. # Make program executable:
  189. add_executable( program main.cpp )
  190. target_link_libraries( program PRIVATE gsl-lite )
  191. ```
  192. </p></details>
  193. Version semantics
  194. -----------------
  195. *gsl-lite* strives to follow [Semantic Versioning](https://semver.org/) guidelines. Although we are still in the "initial development" stage (version 0\.*), we generally maintain
  196. [API](https://en.wikipedia.org/wiki/Application_programming_interface) and [ABI](https://en.wikipedia.org/wiki/Application_binary_interface) compatibility and avoid breaking changes in minor and patch releases.
  197. Development of *gsl-lite* happens in the `master` branch. Versioning semantics apply only to tagged releases: there is no stability guarantee between individual commits in the `master` branch, i.e. anything
  198. added since the last tagged release may be renamed, removed, have the semantics changed, etc. without further notice.
  199. A minor-version release will be compatible (in both ABI and API) with the previous minor-version release (with [rare exceptions](https://github.com/gsl-lite/gsl-lite/issues/156) while we're still in version 0.\*).
  200. Thus, once a change is released, it becomes part of the API.
  201. Some of the [configuration options](#configuration-options) affect the API and ABI of *gsl-lite*. Most configuration options exist because a change we wanted to make would have broken backward compatibility,
  202. so many recent changes and improvements are currently opt-in. The current plan is to toggle the default values of these configuration options for the next major version release.
  203. To simplify migration to the next major version, *gsl-lite* 0.36 introduces the notion of *versioned defaults*. By setting the configuration option `gsl_CONFIG_DEFAULTS_VERSION=0` or `gsl_CONFIG_DEFAULTS_VERSION=1`,
  204. a set of version-specific default options can be selected. Alternatively, when consuming *gsl-lite* [as a CMake package](#as-cmake-package), versioned defaults can be selected by linking to the target
  205. `gsl::gsl-lite-v0` or `gsl::gsl-lite-v1` rather than `gsl::gsl-lite`.
  206. The following table gives an overview of the configuration options affected by versioned defaults:
  207. Macro | v0 default | v1 default | |
  208. ------------------------------------------------------------------------------------:|:---------------------------------------------------------|-------------------|-|
  209. [`gsl_FEATURE_OWNER_MACRO`](#gsl_feature_owner_macro1) | 1 | 0 | an unprefixed macro `Owner()` may interfere with user code |
  210. [`gsl_FEATURE_GSL_LITE_NAMESPACE`](#gsl_feature_gsl_lite_namespace0) | 0 | 1 | cf. [Using *gsl-lite* in libraries](#using-gsl-lite-in-libraries) |
  211. [`gsl_CONFIG_DEPRECATE_TO_LEVEL`](#gsl_config_deprecate_to_level0) | 0 | 6 | |
  212. [`gsl_CONFIG_INDEX_TYPE`](#gsl_config_index_typegsl_config_span_index_type) | `gsl_CONFIG_SPAN_INDEX_TYPE` (defaults to `std::size_t`) | `std::ptrdiff_t` | the GSL specifies `gsl::index` to be a signed type, and M-GSL also uses `std::ptrdiff_t` |
  213. [`gsl_CONFIG_ALLOWS_SPAN_COMPARISON`](#gsl_config_allows_span_comparison1) | 1 | 0 | C++20 `std::span<>` does not support comparison because semantics (deep vs. shallow) are unclear |
  214. [`gsl_CONFIG_NOT_NULL_EXPLICIT_CTOR`](#gsl_config_not_null_explicit_ctor0) | 0 | 1 | cf. reasoning in [M-GSL/#395](https://github.com/Microsoft/GSL/issues/395) (note that `not_null<>` in M-GSL has an implicit constructor, cf. [M-GSL/#699](https://github.com/Microsoft/GSL/issues/699)) |
  215. [`gsl_CONFIG_TRANSPARENT_NOT_NULL`](#gsl_config_transparent_not_null0) | 0 | 1 | enables conformant behavior for `not_null<>::get()` |
  216. [`gsl_CONFIG_NARROW_THROWS_ON_TRUNCATION`](#gsl_config_narrow_throws_on_truncation0) | 0 | 1 | enables conformant behavior for `narrow<>()` (cf. [#52](https://github.com/gsl-lite/gsl-lite/issues/52)) |
  217. Note that the v1 defaults are not yet stable; future 0.\* releases may introduce more configuration switches with different version-specific defaults.
  218. Using *gsl-lite* in libraries
  219. -----------------------------
  220. Many features of *gsl-lite* are very useful for defining library interfaces, e.g. spans, precondition checks, or `gsl::not_null<>`. As such, we encourage using *gsl-lite* in your libraries.
  221. However, please mind the following considerations:
  222. - *gsl-lite* is an implementation of the [Guidelines Support Library](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-gsl), which is not a library but a non-formal specification.
  223. There are other libraries implementing the GSL, most notably the [Microsoft GSL](https://github.com/microsoft/GSL/) (herein often referred to as "M-GSL"). Both libraries live in different headers and consist
  224. of unrelated implementations. There is considerable API compatibility between M-GSL and *gsl-lite*, but some differences are inevitable because the GSL specification is rather loose and informal, and because
  225. both implementations take some liberties at interpreting and extending the specification (cf. e.g. [#6](https://github.com/gsl-lite/gsl-lite/issues/6), [#52](https://github.com/gsl-lite/gsl-lite/issues/52),
  226. [#153](https://github.com/gsl-lite/gsl-lite/issues/153)). Also, the ABIs of *gsl-lite* and M-GSL are generally incompatible.
  227. - It is not clear whether the GSL specification envisions that multiple implementations of the specification should coexist (cf. [CppCoreGuidelines/#1519](https://github.com/isocpp/CppCoreGuidelines/issues/1519)),
  228. but because all existing implementations currently live in the same `namespace gsl`, using more than one GSL implementation in the same target will usually fail with compile/link errors. This is clearly
  229. an impediment for using either in a library because the library would thereby force its consumers to pick the same GSL implementation.
  230. - The API and ABI of *gsl-lite* can be altered by some of the [configuration options](#configuration-options). We consider the availability of these options a strength of *gsl-lite*, but the lack
  231. of an option-invariant API and ABI is another burden for libraries, which may or may not depend on a particular choice of configuration settings and implicitly force these upon their users.
  232. Our goal is to make *gsl-lite* suitable for use in libraries; we want to address all of these concerns in the next major version. But if you want to use *gsl-lite* in a library today, we recommend to
  233. - use version-1 defaults (cf. [Version semantics](#version-semantics))
  234. - include the new header \<gsl-lite/gsl-lite.hpp\> rather than \<gsl/gsl-lite.hpp\>
  235. - refer to the new `namespace gsl_lite` instead of `namespace gsl` (or define a `namespace gsl = ::gsl_lite;` alias in your own namespace)
  236. - use the prefixed contract checking macros `gsl_Expects()`/`gsl_Ensures()` rather than the unprefixed `Expects()`/`Ensures()`
  237. (M-GSL prefixes its macros with uppercase `GSL_`; we traditionally consider lowercase `gsl_` the realm of *gsl-lite*)
  238. - avoid any changes to the configuration options
  239. Example:
  240. ```cmake
  241. # my-statistics-lib/CMakeLists.txt
  242. find_package( gsl-lite 0.37 REQUIRED )
  243. add_library( my-statistics-lib STATIC mean.cpp )
  244. target_link_libraries( my-statistics-lib PUBLIC gsl::gsl-lite-v1 )
  245. ```
  246. ```c++
  247. // my-statistics-lib/include/my-statistics-lib/mean.hpp
  248. #include <gsl-lite/gsl-lite.hpp> // instead of <gsl/gsl-lite.hpp>
  249. namespace my_statistics_lib {
  250. namespace gsl = ::gsl_lite; // convenience alias
  251. double mean( gsl::span<double const> elements )
  252. {
  253. gsl_Expects( !elements.empty() ); // instead of Expects()
  254. ...
  255. }
  256. } // namespace my_statistics_lib
  257. ```
  258. The idea is that *gsl-lite* will move all its definitions to `namespace gsl_lite` in the next major version, and provide a `namespace gsl` with aliases only if the traditional header \<gsl/gsl-lite.hpp\> is
  259. included. This way, any code that only uses the new header \<gsl-lite/gsl-lite.hpp\> will not risk collision with M-GSL.
  260. Configuration options
  261. ---------------------
  262. **Contents**
  263. - [API macro](#api-macro)
  264. - [Standard selection macro](#standard-selection-macro)
  265. - [Feature selection macros](#feature-selection-macros)
  266. - [Contract checking configuration macros](#contract-checking-configuration-macros)
  267. - [Microsoft GSL compatibility macros](#microsoft-gsl-compatibility-macros)
  268. - [Other configuration macros](#other-configuration-macros)
  269. ### API macro
  270. #### `gsl_api`
  271. Functions in *gsl-lite* are decorated with `gsl_api` where appropriate. **By default `gsl_api` is defined empty for non-CUDA platforms and `__host__ __device__` for the CUDA platform.** Define this macro to specify your own function decoration.
  272. ### Standard selection macro
  273. #### `gsl_CPLUSPLUS`
  274. Define this macro to override the auto-detection of the supported C++ standard if your compiler does not set the `__cplusplus` macro correctly.
  275. ### Feature selection macros
  276. #### `gsl_FEATURE_WITH_CONTAINER_TO_STD=99`
  277. Define this to the highest C++ standard (98, 3, 11, 14, 17, 20) you want to include tagged-construction via `with_container`. **Default is 99 for inclusion with any standard.**
  278. #### `gsl_FEATURE_MAKE_SPAN_TO_STD=99`
  279. Define this to the highest C++ standard (98, 3, 11, 14, 17, 20) you want to include `make_span()` creator functions. **Default is 99 for inclusion with any standard.**
  280. #### `gsl_FEATURE_BYTE_SPAN_TO_STD=99`
  281. Define this to the highest C++ standard (98, 3, 11, 14, 17, 20) you want to include `byte_span()` creator functions. **Default is 99 for inclusion with any standard.**
  282. #### `gsl_FEATURE_IMPLICIT_MACRO=0`
  283. Define this macro to 1 to provide the `implicit` macro. **Default is 0.**
  284. #### `gsl_FEATURE_OWNER_MACRO=1`
  285. At default macro `Owner()` is defined for all C++ versions. This may be useful to transition from a compiler that doesn't provide alias templates to one that does. Define this macro to 0 to omit the `Owner()` macro. **Default is 1.**
  286. #### `gsl_FEATURE_EXPERIMENTAL_RETURN_GUARD=0`
  287. Provide experimental types `final_action_return` and `final_action_error` and convenience functions `on_return()` and `on_error()`. **Default is 0.**
  288. #### `gsl_FEATURE_GSL_LITE_NAMESPACE=0`
  289. Define this to additionally define a `namespace gsl_lite` with most of the *gsl-lite* API available, cf. [Using *gsl-lite* in libraries](#using-gsl-lite-in-libraries). **Default is 0.**
  290. ### Contract checking configuration macros
  291. *gsl-lite* provides contract violation response control as originally suggested in proposal [N4415](http://wg21.link/n4415), with some refinements inspired by [P1710](http://wg21.link/P1710)/[P1730](http://wg21.link/P1730).
  292. There are four macros for expressing pre- and postconditions:
  293. - `gsl_Expects()` for simple preconditions
  294. - `gsl_Ensures()` for simple postconditions
  295. - `gsl_ExpectsAudit()` for preconditions that are expensive or include potentially opaque function calls
  296. - `gsl_EnsuresAudit()` for postconditions that are expensive or include potentially opaque function calls
  297. The macros `Expects()` and `Ensures()` are also provided as aliases for `gsl_Expects()` and `gsl_Ensures()`.
  298. The following macros control whether contracts are checked at runtime:
  299. - **`gsl_CONFIG_CONTRACT_CHECKING_AUDIT`**
  300. Define this macro to have all contracts checked at runtime.
  301. - **`gsl_CONFIG_CONTRACT_CHECKING_ON` (default)**
  302. Define this macro to have contracts expressed with `gsl_Expects()` and `gsl_Ensures()` checked at runtime, and contracts expressed with `gsl_ExpectsAudit()` and `gsl_EnsuresAudit()` not checked and not evaluated at runtime. **This is the default.**
  303. - **`gsl_CONFIG_CONTRACT_CHECKING_OFF`**
  304. Define this macro to disable all runtime checking of contracts.
  305. The following macros can be used to selectively disable checking for a particular kind of contract:
  306. - **`gsl_CONFIG_CONTRACT_CHECKING_EXPECTS_OFF`**
  307. Define this macro to disable runtime checking of precondition contracts expressed with `gsl_Expects()` and `gsl_ExpectsAudit()`.
  308. - **`gsl_CONFIG_CONTRACT_CHECKING_ENSURES_OFF`**
  309. Define this macro to disable runtime checking of precondition contracts expressed with `gsl_Ensures()` and `gsl_EnsuresAudit()`.
  310. The following macros control the handling of runtime contract violations:
  311. - **`gsl_CONFIG_CONTRACT_VIOLATION_TERMINATES` (default)**
  312. Define this macro to call `std::terminate()` on a GSL contract violation in `gsl_Expects()`, `gsl_ExpectsAudit()`, `gsl_Ensures()`, and `gsl_EnsuresAudit()`. **This is the default.**
  313. - **`gsl_CONFIG_CONTRACT_VIOLATION_THROWS`**
  314. Define this macro to throw a std::runtime_exception-derived exception `gsl::fail_fast` instead of calling `std::terminate()` on a GSL contract violation in `gsl_Expects()`, `gsl_ExpectsAudit()`, `gsl_Ensures()`, and `gsl_EnsuresAudit()`.
  315. - **`gsl_CONFIG_CONTRACT_VIOLATION_CALLS_HANDLER`**
  316. Define this macro to call a user-defined handler function `gsl::fail_fast_assert_handler()` instead of calling `std::terminate()` on a GSL contract violation in `gsl_Expects()`, `gsl_ExpectsAudit()`, `gsl_Ensures()`, and `gsl_EnsuresAudit()`. The user must provide a definition of the following function:
  317. ```c++
  318. namespace gsl {
  319. gsl_api void fail_fast_assert_handler(
  320. char const * const expression, char const * const message,
  321. char const * const file, int line );
  322. }
  323. ```
  324. The following macros control what happens with contract checks not enforced at runtime:
  325. - **`gsl_CONFIG_UNENFORCED_CONTRACTS_ELIDE` (default)**
  326. Define this macro to disable all runtime checking and evaluation of contracts. **This is the default.**
  327. - **`gsl_CONFIG_UNENFORCED_CONTRACTS_ASSUME`**
  328. Define this macro to let the compiler assume that contracts expressed with `gsl_Expects()` and `gsl_Ensures()` always hold true, and to have contracts expressed with `gsl_ExpectsAudit()` and `gsl_EnsuresAudit()` not checked and not evaluated at runtime. With this setting, contract violations lead to undefined behavior, which gives the compiler more opportunities for optimization but can be dangerous if the code is not prepared for it.
  329. Note that the distinction between regular and audit-level contracts is subtly different from the C++2a Contracts proposals. When `gsl_CONFIG_UNENFORCED_CONTRACTS_ASSUME` is defined, the compiler is instructed that the
  330. condition expressed by regular contracts can be assumed to hold true. This is meant to be an aid for the optimizer; runtime evaluation of the condition is not desired. However, because the GSL implements contract checks
  331. with macros rather than as a language feature, it cannot reliably suppress runtime evaluation of a condition for all compilers. If the contract comprises a function call which is opaque to the compiler, many compilers will
  332. generate the runtime function call.
  333. Therefore, `gsl_Expects()` and `gsl_Ensures()` should be used only for conditions that can be proven side-effect-free by the compiler, and `gsl_ExpectsAudit()` and `gsl_EnsuresAudit()` for everything else. In practice, this implies that
  334. `gsl_Expects()` and `gsl_Ensures()` should only be used for simple comparisons of scalar values, for simple inlineable getters, and for comparisons of class objects with trivially inlineable comparison operators.
  335. Example:
  336. ```c++
  337. template< class RandomIt >
  338. auto median( RandomIt first, RandomIt last )
  339. {
  340. // Comparing iterators for equality boils down to a comparison of pointers. An optimizing
  341. // compiler will inline the comparison operator and understand that the comparison is free
  342. // of side-effects, and hence generate no code in gsl_CONFIG_UNENFORCED_CONTRACTS_ASSUME mode.
  343. gsl_Expects( first != last );
  344. // Verifying that a range of elements is sorted may be an expensive operation, and we
  345. // cannot trust the compiler to understand that it is free of side-effects, so we use an
  346. // audit-level contract check.
  347. gsl_ExpectsAudit( std::is_sorted( first, last ) );
  348. auto count = last - first;
  349. return count % 2 != 0
  350. ? first[ count / 2 ]
  351. : std::midpoint( first[ count / 2 ], first[ count / 2 + 1 ] );
  352. }
  353. ```
  354. ### Microsoft GSL compatibility macros
  355. #### `GSL_UNENFORCED_ON_CONTRACT_VIOLATION`
  356. Equivalent to defining `gsl_CONFIG_CONTRACT_CHECKING_OFF`.
  357. #### `GSL_TERMINATE_ON_CONTRACT_VIOLATION`
  358. Equivalent to defining `gsl_CONFIG_CONTRACT_VIOLATION_TERMINATES`.
  359. #### `GSL_THROW_ON_CONTRACT_VIOLATION`
  360. Equivalent to defining `gsl_CONFIG_CONTRACT_VIOLATION_THROWS`.
  361. ### Other configuration macros
  362. #### `gsl_CONFIG_DEPRECATE_TO_LEVEL=0`
  363. Define this to and including the level you want deprecation; see table [Deprecation](#deprecation) below. **Default is 0 for no deprecation.**
  364. #### `gsl_CONFIG_SPAN_INDEX_TYPE=std::size_t`
  365. Define this macro to the type to use for indices in `span<>` and `basic_string_span<>`. Microsoft GSL uses `std::ptrdiff_t`. **Default for *gsl-lite* is `std::size_t`.**
  366. #### `gsl_CONFIG_INDEX_TYPE=gsl_CONFIG_SPAN_INDEX_TYPE`
  367. Define this macro to the type to use for `gsl::index`. Microsoft's GSL uses `std::ptrdiff_t`. **Default for *gsl-lite* is `std::size_t`.**
  368. #### `gsl_CONFIG_NOT_NULL_EXPLICIT_CTOR=0`
  369. Define this macro to 1 to make `not_null<>`'s constructor explicit. **Default is 0.** Note that in Microsoft's GSL the constructor is explicit. For implicit construction you can also use the *gsl-lite*-specific `not_null<>`-derived class `not_null_ic<>`.
  370. #### `gsl_CONFIG_TRANSPARENT_NOT_NULL=0`
  371. Define this macro to 1 to have `not_null<>` support typical member functions of the underlying smart pointer transparently (currently `get()`), while adding precondition checks. This is conformant behavior but may be incompatible with older code which expects that `not_null<>::get()` returns the underlying pointer itself. **Default is 0.**
  372. #### `gsl_CONFIG_NOT_NULL_GET_BY_CONST_REF=0`
  373. Define this macro to 1 to have the legacy non-transparent version of `not_null<>::get()` return `T const &` instead of `T`. This may improve performance with types that have an expensive copy-constructor. This macro may not be defined if `gsl_CONFIG_TRANSPARENT_NOT_NULL` is 1. **Default is 0 for `T`.**
  374. #### `gsl_CONFIG_ALLOWS_SPAN_COMPARISON=1`
  375. Define this macro to 0 to omit the ability to compare spans. C++20 `std::span<>` does not support comparison because semantics (deep vs. shallow) are unclear. **Default is 1.**
  376. #### `gsl_CONFIG_ALLOWS_NONSTRICT_SPAN_COMPARISON=1`
  377. Define this macro to 0 to omit the ability to compare spans of different types, e.g. of different const-volatile-ness. To be able to compare a string_span with a cstring_span, non-strict span comparison must be available. **Default is 1.**
  378. #### `gsl_CONFIG_ALLOWS_UNCONSTRAINED_SPAN_CONTAINER_CTOR=0`
  379. Define this macro to 1 to add the unconstrained span constructor for containers for pre-C++11 compilers that cannot constrain the constructor. This constructor may prove too greedy and interfere with other constructors. **Default is 0.**
  380. Note: an alternative is to use the constructor tagged `with_container`: `span<V> s(gsl::with_container, cont)`.
  381. #### `gsl_CONFIG_NARROW_THROWS_ON_TRUNCATION=0`
  382. Define this macro to 1 to have `narrow<>()` always throw a `narrowing_error` exception if the narrowing conversion loses information due to truncation. If `gsl_CONFIG_NARROW_THROWS_ON_TRUNCATION` is 0 and `gsl_CONFIG_CONTRACT_VIOLATION_THROWS` is not defined, `narrow<>()` instead calls `std::terminate()` on information loss. **Default is 0.**
  383. #### `gsl_CONFIG_CONFIRMS_COMPILATION_ERRORS=0`
  384. Define this macro to 1 to experience the by-design compile-time errors of the GSL components in the test suite. **Default is 0.**
  385. Features
  386. --------
  387. See also section [GSL: Guidelines Support Library](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#S-gsl) of the C++ Core Guidelines [9].
  388. Feature / library | GSL | M-GSL | *gsl-lite* | Notes |
  389. ----------------------------|:-------:|:-------:|:----------:|:------|
  390. **1.Lifetime&nbsp;safety** | &nbsp; | &nbsp; | &nbsp; | &nbsp; |
  391. **1.1 Indirection** | &nbsp; | &nbsp; | &nbsp; | &nbsp; |
  392. `not_null<>` | | | | Wrap any indirection and enforce non-null,<br>see also [Other configuration macros](#other-configuration-macros) |
  393. `not_null_ic<>` | - | - | | not_null with implicit constructor, allowing [copy-initialization](https://en.cppreference.com/w/cpp/language/copy_initialization) |
  394. **1.2 Ownership** | &nbsp; | &nbsp; | &nbsp; | &nbsp; |
  395. `owner<>` | | | &nbsp;C++11 | Owned raw pointers |
  396. `Owner()` | - | - | | Macro for pre-C++11;<br>see also [Feature selection macros](#feature-selection-macros) |
  397. `unique_ptr<>` | | | &nbsp;C++11 | `std::unique_ptr<>` |
  398. `unique_ptr<>` | - | - | <&nbsp;C++11 | VC10, VC11 |
  399. `shared_ptr<>` | | | &nbsp;C++11 | `std::shared_ptr<>` |
  400. `shared_ptr<>` | - | - | <&nbsp;C++11 | VC10, VC11 |
  401. `stack_array<>` | | - | - | A stack-allocated array, fixed size |
  402. `dyn_array<>` | ? | - | - | A heap-allocated array, fixed size |
  403. **2.Bounds&nbsp;safety** | &nbsp; | &nbsp; | &nbsp; | &nbsp; |
  404. **2.1 Tag Types** | &nbsp; | &nbsp; | &nbsp; | &nbsp; |
  405. `zstring` | | | | a `char*` (C-style string) |
  406. `wzstring` | - | | | a `wchar_t*` (C-style string) |
  407. `czstring` | | | | a `const char*` (C-style string) |
  408. `cwzstring` | - | | | a `const wchar_t*` (C-style string) |
  409. `**2.2 Views** | &nbsp; | &nbsp; | &nbsp; | &nbsp; |
  410. `span<>` | ✓ | ✓ | 1D&nbsp;views | A view of contiguous T's, replace (*,len),<br>see also proposal [p0122](http://wg21.link/p0122) |
  411. `span_p<>` | ✓ | - | - | A view of contiguous T's that ends at the first element for which predicate(*p) is true |
  412. `make_span()` | - | ✓ | ✓ | Create a span |
  413. `byte_span()` | - | - | ✓ | Create a span of bytes from a single object |
  414. `as_bytes()` | - | ✓ | ✓ | A span as bytes |
  415. `as_writable_bytes` | - | ✓ | ✓ | A span as writable bytes |
  416. `basic_string_span<>` | - | ✓ | ✓ | See also proposal [p0123](http://wg21.link/p0123) |
  417. `string_span` | ✓ | ✓ | ✓ | `basic_string_span< char >` |
  418. `wstring_span` | - | ✓ | ✓ | `basic_string_span< wchar_t >` |
  419. `cstring_span` | ✓ | ✓ | ✓ | `basic_string_span< const char >` |
  420. `cwstring_span` | - | ✓ | ✓ | `basic_string_span< const wchar_t >` |
  421. `zstring_span` | - | ✓ | ✓ | `basic_zstring_span< char >` |
  422. `wzstring_span` | - | ✓ | ✓ | `basic_zstring_span< wchar_t >` |
  423. `czstring_span` | - | ✓ | ✓ | `basic_zstring_span< const char >` |
  424. `cwzstring_span` | - | ✓ | ✓ | `basic_zstring_span< const wchar_t >` |
  425. `ensure_z()` | - | ✓ | ✓ | Create a `cstring_span` or `cwstring_span` |
  426. `to_string()` | - | ✓ | ✓ | Convert a `string_span` to `std::string` or `std::wstring` |
  427. **2.3 Indexing** | &nbsp; | &nbsp; | &nbsp; | &nbsp; |
  428. `at()` | ✓ | ✓ | ≥&nbsp;C++11 | Bounds-checked way of accessing<br>static arrays, `std::array<>`, `std::vector<>` |
  429. `at()` | - | - | <&nbsp;C++11 | static arrays, `std::vector<>`<br>`std::array<>` : VC11 |
  430. **3. Assertions** | &nbsp; | &nbsp; | &nbsp; | &nbsp; |
  431. `Expects()` | ✓ | ✓ | ✓ | Precondition assertion |
  432. `Ensures()` | ✓ | ✓ | ✓ | Postcondition assertion |
  433. `gsl_Expects()` | - | - | ✓ | Precondition assertion |
  434. `gsl_Ensures()` | - | - | ✓ | Postcondition assertion |
  435. `gsl_ExpectsAudit()` | - | - | ✓ | Audit-level precondition assertion |
  436. `gsl_EnsuresAudit()` | - | - | ✓ | Audit-level postcondition assertion |
  437. **4. Utilities** | &nbsp; | &nbsp; | &nbsp; | &nbsp; |
  438. `index` | ✓ | ✓ | ✓ | type for container indexes and subscripts, <br>see [Other configuration macros](#other-configuration-macros) |
  439. `dim` | - | - | ✓ | type for container sizes |
  440. `stride` | - | - | ✓ | type for index strides |
  441. `diff` | - | - | ✓ | type for index differences |
  442. `byte` | - | ✓ | ✓ | byte type, see also proposal [p0298](http://wg21.link/p0298) |
  443. `final_action<>` | ✓ | ✓ | ≥&nbsp;C++11 | Action at the end of a scope |
  444. `final_action` | - | - | <&nbsp;C++11 | Currently only `void(*)()` |
  445. `finally()` | ✓ | ✓ | ≥&nbsp;C++11 | Make a `final_action<>` |
  446. `finally()` | - | - | <&nbsp;C++11 | Make a `final_action` |
  447. `final_action_return` | - | - | <&nbsp;C++11 | Currently only `void(*)()`, [experimental](#feature-selection-macros) |
  448. `on_return()` | - | - | ≥&nbsp;C++11 | Make a `final_action_return<>, [experimental](#feature-selection-macros) |
  449. `on_return()` | - | - | <&nbsp;C++11 | Make a `final_action_return, [experimental](#feature-selection-macros) |
  450. `final_action_error` | - | - | <&nbsp;C++11 | Currently only `void(*)()`, [experimental](#feature-selection-macros) |
  451. `on_error()` | - | - | ≥&nbsp;C++11 | Make a `final_action_error<>`, [experimental](#feature-selection-macros) |
  452. `on_error()` | - | - | <&nbsp;C++11 | Make a `final_action_error`, [experimental](#feature-selection-macros) |
  453. `narrow_cast<>` | ✓ | ✓ | ✓ | Searchable narrowing casts of values |
  454. `narrow<>()` | ✓ | ✓ | ✓ | Checked narrowing cast |
  455. `narrow_failfast<>()` | - | - | ✓ | Fail-fast narrowing cast |
  456. `[[implicit]]` | ✓ | - | C++?? | Symmetric with explicit |
  457. `implicit` | - | - | ✓ | Macro, see [Feature selection macros](#feature-selection-macros) |
  458. `move_owner` | ? | - | - | ... |
  459. **5. Algorithms** | &nbsp; | &nbsp; | &nbsp; | &nbsp; |
  460. `copy()` | &nbsp; | &nbsp; | &nbsp; | Copy from source span to destination span |
  461. `size()` | &nbsp; | &nbsp; | &nbsp; | Size of span, unsigned |
  462. `ssize()` | &nbsp; | &nbsp; | &nbsp; | Size of span, signed |
  463. **6. Concepts** | &nbsp; | &nbsp; | &nbsp; | &nbsp; |
  464. ... | &nbsp; | &nbsp; | &nbsp; | &nbsp; |
  465. Note: *gsl-lite* treats VC12 (VS2013) and VC14 (VS2015) as C++11 (`gsl_CPP11_OR_GREATER`: 1).
  466. Deprecation
  467. -----------
  468. The following features are deprecated since the indicated version. See macro [`gsl_CONFIG_DEPRECATE_TO_LEVEL`](#other-configuration-macros) on how to control deprecation using the indicated level.
  469. Version | Level | Feature / Notes |
  470. -------:|:-----:|:----------------|
  471. 0.37.0 | 6 | `as_writeable_bytes()`, call indexing for spans, and `span::at()` |
  472. &nbsp; |&nbsp; | `as_writable_bytes()`, subscript indexing is used |
  473. 0.35.0 | - | `gsl_CONFIG_CONTRACT_LEVEL_ON`, `gsl_CONFIG_CONTRACT_LEVEL_OFF`, `gsl_CONFIG_CONTRACT_LEVEL_EXPECTS_ONLY` and `gsl_CONFIG_CONTRACT_LEVEL_ENSURES_ONLY` |
  474. &nbsp; |&nbsp; | Use `gsl_CONFIG_CONTRACT_CHECKING_ON`, `gsl_CONFIG_CONTRACT_CHECKING_OFF`, `gsl_CONFIG_CONTRACT_CHECKING_ENSURES_OFF`, `gsl_CONFIG_CONTRACT_CHECKING_EXPECTS_OFF` |
  475. 0.31.0 | 5 | `span( std::nullptr_t, index_type )` |
  476. &nbsp; |&nbsp; | `span( pointer, index_type )` is used |
  477. 0.31.0 | 5 | `span( U *, index_type size )` |
  478. &nbsp; |&nbsp; | `span( pointer, index_type )` is used |
  479. 0.31.0 | 5 | `span( U (&arr)[N] )` |
  480. &nbsp; |&nbsp; | `span( element_type (&arr)[N] )` is used |
  481. 0.31.0 | 5 | `span( std::array< U, N > [const] & arr )` |
  482. &nbsp; |&nbsp; | `span( std::array< value_type, N > [const] & arr )` is used |
  483. 0.29.0 | 4 | `span( std::shared_ptr<T> const & p )` |
  484. &nbsp; |&nbsp; | &mdash; |
  485. 0.29.0 | 4 | `span( std::unique_ptr<T> const & p )` |
  486. &nbsp; |&nbsp; | &mdash; |
  487. 0.29.0 | 3 | `span<>::length()` |
  488. &nbsp; |&nbsp; | Use `span<>::size()` |
  489. 0.29.0 | 3 | `span<>::length_bytes()` |
  490. &nbsp; |&nbsp; | Use `span<>::size_bytes()` |
  491. 0.17.0 | 2 | member `span<>::as_bytes()`, `span<>::as_writeable_bytes()` |
  492. &nbsp; |&nbsp; | &mdash; |
  493. 0.7.0 | - | `gsl_CONFIG_ALLOWS_SPAN_CONTAINER_CTOR` |
  494. &nbsp; |&nbsp; | Use `gsl_CONFIG_ALLOWS_UNCONSTRAINED_SPAN_CONTAINER_CTOR`,<br>or consider `span(with_container, cont)`. |
  495. Reported to work with
  496. ---------------------
  497. The table below mentions the compiler versions and platforms *gsl-lite* is reported to work with.
  498. Compiler | OS | Platforms | Versions | CI |
  499. --------------------:|:----------------|-----------|------------------:|----|
  500. GCC | Linux | x64 | 4.7 and newer | [4.7, 4.8, 4.9, 5](https://travis-ci.com/gsl-lite/gsl-lite/), [6, 7, 8, 9, 10](https://dev.azure.com/gsl-lite/gsl-lite/_build?definitionId=1) |
  501. GCC (MinGW) | Windows | x86, x64 | 4.8.4 and newer | |
  502. GCC (DJGPP) | DOSBox, FreeDOS | x86 | 7.2 | |
  503. GCC | MacOS | x64 | 6 and newer | [6, 7, 8, 9, 10](https://dev.azure.com/gsl-lite/gsl-lite/_build?definitionId=1) |
  504. Clang | Linux | x64 | 3.5 and newer | [3.5, 3.6, 3.7, 3.8, 3.9](https://travis-ci.com/gsl-lite/gsl-lite/), [4, 5, 6, 7, 8, 9, 10](https://dev.azure.com/gsl-lite/gsl-lite/_build?definitionId=1) |
  505. Clang | Windows | x64 | 9 and newer | [9, 10](https://dev.azure.com/gsl-lite/gsl-lite/_build?definitionId=1) |
  506. MSVC (Visual Studio) | Windows | x86, x64 | VS 2010 and newer | VS [2010, 2012, 2013, 2015](https://ci.appveyor.com/project/gsl-lite/gsl-lite), [2017, 2019](https://dev.azure.com/gsl-lite/gsl-lite/_build?definitionId=1) |
  507. AppleClang (Xcode) | MacOS | x64 | 7.3 and newer | [7.3, 8, 8.1, 9](https://travis-ci.com/gsl-lite/gsl-lite/), [9.1, 10, 10.0.1, 11, 11.0.3](https://dev.azure.com/gsl-lite/gsl-lite/_build?definitionId=1) |
  508. NVCC (CUDA Toolkit) | Linux, Windows | x64 | 10.2 and newer | [10.2, 11.0](https://dev.azure.com/gsl-lite/gsl-lite/_build?definitionId=1) |
  509. Building the tests
  510. ------------------
  511. To build the tests:
  512. - [CMake](http://cmake.org), version 3.15 or later to be installed and in your PATH.
  513. - A [suitable compiler](#reported-to-work-with).
  514. The [*lest* test framework](https://github.com/martinmoene/lest) is included in the [test folder](test).
  515. The following steps assume that the [*gsl-lite* source code](https://github.com/gsl-lite/gsl-lite) has been cloned into a directory named `C:\gsl-lite`.
  516. 1. Create a directory for the build outputs.
  517. Here we use `C:\gsl-lite\build`.
  518. cd C:\gsl-lite
  519. mkdir build
  520. cd build
  521. 2. Configure the build directory with CMake:
  522. cmake -DGSL_LITE_OPT_BUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Debug ..
  523. 3. Build the test suite:
  524. cmake --build . --config Debug
  525. 4. Run the test suite:
  526. ctest -V -C Debug
  527. All tests should pass, indicating your platform is supported and you are ready to use *gsl-lite*. See the table with [supported types and functions](#features).
  528. Other GSL implementations
  529. -------------------------
  530. - Microsoft. [Guidelines Support Library (GSL)](https://github.com/microsoft/GSL).
  531. - Vicente J. Botet Escriba. [Guidelines Support Library (GSL)](https://github.com/viboes/GSL).
  532. Notes and references
  533. --------------------
  534. ### Proposals, specification
  535. [1] [`std::span<>` on cppreference](https://en.cppreference.com/w/cpp/container/span).
  536. [2] [`std::span<>` in C++20 Working Draft](http://eel.is/c++draft/views).
  537. [3] [P0091 - Template argument deduction for class templates](http://wg21.link/p0091).
  538. [4] [P0122 - span: bounds-safe views for sequences of objects](http://wg21.link/p0122).
  539. [5] [P0123 - string_span: bounds-safe views for sequences of characters](http://wg21.link/p0123).
  540. [6] [P0298 - A byte type definition](http://wg21.link/p0298).
  541. [7] [P0805 - Comparing Containers](http://wg21.link/p0805).
  542. ### Articles
  543. [8] [Standard C++ Foundation](https://isocpp.org/).
  544. [9] Standard C++ Foundation. [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines).
  545. [10] Microsoft. [Guidelines Support Library (GSL)](https://github.com/microsoft/gsl).
  546. [11] Bjarne Stroustrup. [Writing good C++14 (PDF)](https://github.com/isocpp/CppCoreGuidelines/raw/master/talks/Stroustrup%20-%20CppCon%202015%20keynote.pdf) &mdash; [Video](https://www.youtube.com/watch?t=9&v=1OEu9C51K2A). CppCon 2015.
  547. [12] Herb Sutter. [Writing good C++14&hellip; By default (PDF)](https://github.com/isocpp/CppCoreGuidelines/raw/master/talks/Sutter%20-%20CppCon%202015%20day%202%20plenary%20.pdf) &mdash; [Video](https://www.youtube.com/watch?v=hEx5DNLWGgA). CppCon 2015.
  548. [13] Gabriel Dos Reis. [Contracts for Dependable C++ (PDF)](https://github.com/isocpp/CppCoreGuidelines/raw/master/talks/Contracts-for-Dependable-C%2B%2B.pdf) &mdash; Video. CppCon 2015.
  549. [14] Bjarne Stroustrup et al. [A brief introduction to C++’s model for type- and resource-safety](https://github.com/isocpp/CppCoreGuidelines/raw/master/docs/Introduction%20to%20type%20and%20resource%20safety.pdf).
  550. [15] Herb Sutter and Neil MacIntosh. [Lifetime Safety: Preventing Leaks and Dangling](https://github.com/isocpp/CppCoreGuidelines/raw/master/docs/Lifetimes%20I%20and%20II%20-%20v0.9.1.pdf). 21 Sep 2015.
  551. ### Compiler feature testing
  552. [16] cppreference.com. [Feature testing](https://en.cppreference.com/w/cpp/feature_test).
  553. ### C++ features in various compilers
  554. [17] cppreference.com. [C++ compiler support](https://en.cppreference.com/w/cpp/compiler_support).
  555. Appendix
  556. --------
  557. <a id="a1"></a>
  558. ### A.1 Compile-time information
  559. In the test runner, the version of *gsl-lite* is available via tag `[.version]`. The following tags are available for information on the compiler and on the C++ standard library used: `[.compiler]`, `[.stdc++]`, `[.stdlanguage]` and `[.stdlibrary]`.
  560. <a id="a2"></a>
  561. ### A.2 *gsl-lite* test specification
  562. <details>
  563. <summary>click to expand</summary>
  564. <p>
  565. ```
  566. gsl_Expects(): Allows a true expression
  567. gsl_Ensures(): Allows a true expression
  568. gsl_Expects(): Terminates on a false expression
  569. gsl_Ensures(): Terminates on a false expression
  570. gsl_ExpectsAudit(): Allows a true expression
  571. gsl_EnsuresAudit(): Allows a true expression
  572. gsl_ExpectsAudit(): Terminates on a false expression in AUDIT mode
  573. gsl_EnsuresAudit(): Terminates on a false expression in AUDIT mode
  574. at(): Terminates access to non-existing C-array elements
  575. at(): Terminates access to non-existing std::array elements (C++11)
  576. at(): Terminates access to non-existing std::vector elements
  577. at(): Terminates access to non-existing std::initializer_list elements (C++11)
  578. at(): Terminates access to non-existing gsl::span elements
  579. at(): Allows to access existing C-array elements
  580. at(): Allows to access existing std::array elements (C++11)
  581. at(): Allows to access existing std::vector elements
  582. at(): Allows to access std::initializer_list elements (C++11)
  583. at(): Allows to access gsl::span elements
  584. byte: Allows to construct from integral via static cast (C++17)
  585. byte: Allows to construct from integral via byte() (C++17)
  586. byte: Allows to construct from integral via to_byte()
  587. byte: Allows to convert to integral via to_integer()
  588. byte: Allows comparison operations
  589. byte: Allows bitwise or operation
  590. byte: Allows bitwise and operation
  591. byte: Allows bitwise x-or operation
  592. byte: Allows bitwise or assignment
  593. byte: Allows bitwise and assignment
  594. byte: Allows bitwise x-or assignment
  595. byte: Allows shift-left operation
  596. byte: Allows shift-right operation
  597. byte: Allows shift-left assignment
  598. byte: Allows shift-right assignment
  599. byte: Provides constexpr non-assignment operations (C++11)
  600. byte: Provides constexpr assignment operations (C++14)
  601. byte: Provides hash support (C++11)
  602. conjunction<> and disjunction<>: Short-circuiting is handled correctly
  603. conjunction<> and disjunction<>: First suitable type is chosen as base
  604. span<>: free comparation functions fail for different const-ness [issue #32]
  605. span<>: constrained container constructor suffers hard failure for arguments with reference-returning data() function [issue #242]
  606. byte: aliasing rules lead to undefined behaviour when using enum class [issue #34](GSL issue #313, PR #390)
  607. string_span<>: must not include terminating '\0' [issue #53]
  608. string_span<>: to_string triggers SFINAE errors on basic_string_span's move & copy constructor with Clang-3.9 (define gsl_CONFIG_CONFIRMS_COMPILATION_ERRORS) [issue #53a]
  609. narrow<>(): Allows narrowing double to float without MSVC level 4 warning C4127: conditional expression is constant [issue #115]
  610. detail::is_compatible_container<>: Not a proper type trait [PR #238]
  611. not_null<>: Disallows default construction (define gsl_CONFIG_CONFIRMS_COMPILATION_ERRORS)
  612. not_null<>: Disallows construction from nullptr_t, NULL or 0 (define gsl_CONFIG_CONFIRMS_COMPILATION_ERRORS)
  613. not_null<>: Disallows construction from a unique pointer to underlying type (define gsl_CONFIG_CONFIRMS_COMPILATION_ERRORS)
  614. not_null<>: Layout is compatible to underlying type
  615. not_null<>: Convertibility is correctly reported by type traits
  616. not_null<>: Copyability and assignability are correctly reported by type traits
  617. not_null<>: Disallows assignment from unrelated pointers (define gsl_CONFIG_CONFIRMS_COMPILATION_ERRORS)
  618. not_null<>: Terminates construction from a null pointer value (raw pointer)
  619. not_null<>: Terminates construction from related pointer types for null pointer value (raw pointer)
  620. not_null<>: Terminates assignment from a null pointer value (raw pointer)
  621. not_null<>: Terminates assignment from related pointer types for null pointer value (raw pointer)
  622. not_null<>: Allows to construct from a non-null underlying pointer (raw pointer)
  623. not_null<>: Returns underlying pointer with get() (raw point