The Guidelines Support Library (GSL) contains functions and types that are suggested for use by the
C++ Core Guidelines maintained by the Standard C++ Foundation.
This repo contains Microsoft's implementation of GSL.
The entire implementation is provided inline in the headers under the gsl directory. The implementation generally assumes a platform that implements C++14 support.
While some types have been broken out into their own headers (e.g. gsl/span),
it is simplest to just include gsl/gsl and gain access to the entire library.
NOTE: We encourage contributions that improve or refine any of the types in this library as well as ports to
other platforms. Please see CONTRIBUTING.md for more information about contributing.
This project makes use of the Google Test testing library. Please see the ThirdPartyNotices.txt file for details regarding the licensing of Google Test.
Supported features
Microsoft GSL implements the following from the C++ Core Guidelines:
restricts a pointer / smart pointer to hold non-null values
span
☑
a view over a contiguous sequence of memory. Based on the standardized verison of std::span, however gsl::span enforces bounds checking. See the wiki for additional information.
span_p
☐
spans a range starting from a pointer to the first place for which the predicate is true
basic_zstring
☑
A pointer to a C-string (zero-terminated array) with a templated char type
zstring
☑
An alias to basic_zstring with dynamic extent and a char type of char
czstring
☑
An alias to basic_zstring with dynamic extent and a char type of const char
wzstring
☑
An alias to basic_zstring with dynamic extent and a char type of wchar_t
cwzstring
☑
An alias to basic_zstring with dynamic extent and a char type of const wchar_t
u16zstring
☑
An alias to basic_zstring with dynamic extent and a char type of char16_t
cu16zstring
☑
An alias to basic_zstring with dynamic extent and a char type of const char16_t
u32zstring
☑
An alias to basic_zstring with dynamic extent and a char type of char32_t
cu32zstring
☑
An alias to basic_zstring with dynamic extent and a char type of const char32_t
The GSL officially supports the latest and previous major versions of VS with MSVC & LLVM, GCC, Clang, and XCode with Apple-Clang.
Within these two major versions, we try to target the latest minor updates / revisions (although this may be affected by
delays between a toolchain's release and when it becomes widely available for use).
Below is a table showing the versions currently being tested.
The GSL port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.
Using the libraries
As the types are entirely implemented inline in headers, there are no linking requirements.
You can copy the gsl directory into your source tree so it is available
to your compiler, then include the appropriate headers in your program.
Alternatively set your compiler's include path flag to point to the GSL development folder (c:\GSL\include in the example above) or installation folder (after running the install). Eg.
MSVC++
/I c:\GSL\include
GCC/clang
-I$HOME/dev/GSL/include
Include the library using:
#include <gsl/gsl>
Usage in CMake
The library provides a Config file for CMake, once installed it can be found via
find_package(Microsoft.GSL CONFIG)
Which, when successful, will add library target called Microsoft.GSL::GSL which you can use via the usual
target_link_libraries mechanism.
FetchContent
If you are using cmake version 3.11+ you can use the offical FetchContent module.
This allows you to easily incorporate GSL into your project.
# NOTE: This example uses cmake version 3.14 (FetchContent_MakeAvailable).# Since it streamlines the FetchContent processcmake_minimum_required(VERSION 3.14)
include(FetchContent)
# In this example we are picking a specific tag.# You can also pick a specific commit, if you need to.
FetchContent_Declare(GSL
GIT_REPOSITORY "https://github.com/microsoft/GSL"
GIT_TAG "v3.1.0"
)
FetchContent_MakeAvailable(GSL)
# Now you can link against the GSL interface libraryadd_executable(foobar)
# Link against the interface library (IE header only library)target_link_libraries(foobar PRIVATE GSL)
Debugging visualization support
For Visual Studio users, the file GSL.natvis in the root directory of the repository can be added to your project if you would like more helpful visualization of GSL types in the Visual Studio debugger than would be offered by default.
If you are using cmake this will be done automatically for you.
See 'GSL_VS_ADD_NATIVE_VISUALIZERS'
请发表评论