c++c++11library-designcpp-core-guidelines

What purpose does `gsl::string_span` aim at?


During reading Microsoft's implementation of Cpp Core Guidelines, I run across two questions:

  1. Why is gsl::string_span provided where gsl::span already works well?
  2. Why is gsl::zstring_span provided where std::string is already guaranteed to be null-terminated since C++11?

Any illustrating situations will be highly appreciated.


Solution

    1. span("Hi") is {'H', 'i', '\0'} whereas string_span("Hi") is {'H', 'i'}. string_span checks for the terminating null character and does not include it in the span.
    2. string is owning and spans are not, so comparing them is comparing apples and oranges. zstring_span is a span with the constraint that the last character is a null character. Neither span nor string_span have that constraint.