Search code examples
string-viewguideline-support-librarystring-span

How do gsl::string_span and std::string_view differ?


From what I can gather, gsl::string_span and std::string_view seem to have essentially the same rationale for use. Is that indeed the case? If so, are they effectively identical? If not - how do they differ?

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


Solution

  • How do gsl::string_span and std::string_view differ?

    A rather obvious difference of how they are available, but I'll say it since it is significant: gsl::string_span requires the use of a third party library, while std::string_view is a standard C++ type. On the other hand, the library providing gsl::string_span supports C++14, while std::string_view requires C++17.

    A major design difference is that std::string_view is a const view to the string, and doesn't provide any way of modifying the viewed string, while gsl::string_span does allow non-const access. For example:

    constexpr iterator gsl::string_span::begin() const noexcept
              ^^^^^^^^ note non-const iterator   ^^^^^ also note this
    

    Also note how gsl::string_span allows non-const access even when the span itself is const. in other words, gsl::string_span does not propagate constness. This is the same as std::span and gsl::span.