Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 292
szxwpmj Avatar asked Mar 18 '18 14:03

szxwpmj


1 Answers

  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.
like image 129
Jeff Garrett Avatar answered Sep 28 '22 07:09

Jeff Garrett