Using Guideline Support Library ?

For code related discussions and questions

Using Guideline Support Library ?

Postby Vincent » 30 Oct 2016, 17:38

Is it ok to use the GSL Library ?
This lib is an almost standard headers only lib that aims at providing some useful type not available in the standard like gsl::byte or gsl::span.
gsl::byte is an alias over char but it forbids implicit cast to/from other type making it type safe ; it can be used for instance to represent raw buffer in GL or Vulkan.
gsl::span is likely much more useful. C++17 introduces a new type, string_view, which was initially available in GSL. This types is a "non owning alias" over both std::string and const char*. Functions that reads or modifies a buffer of char can now have a string_view as an argument making them work on both std::string and const char*. The idea is that some objects or scope "own" string or const char* and are responsible for their allocation and deletion. By using string_view a functions can access the char buffer while making it clear it won't resize or clear the string as it doesn't own the data. This extends the idea of ownership introduced by std::unique_ptr and the like which helps avoid leaks and read after free.
gsl::span does the same but for array-like type as std::array std::vector, plain C array or dynamic array.

It's intended to be understandable by static analyser Tools like the one of Microsoft or clang-tidy (they both already supports GSL). Pointers can be used for single object or array or object and gsl::span tells the Tools that the data is an array.
Posts: 103
Joined: 06 Aug 2016, 17:24

Return to Coding