12

Boost provides two different implementations of string_view, which will be a part of C++17:

  • boost::string_ref in utility/string_ref.hpp
  • boost::string_view in core/string_view.hpp

Are there any significant differences between these? Which should be preferred going forward?

Note: I noticed in Boost 1.61, boost::log has deprecated string_ref in favor of string_view; perhaps that's an indicator? (http://www.boost.org/users/history/version_1_61_0.html)

leecbaker
  • 3,283
  • 2
  • 31
  • 48

2 Answers2

13

Funnily enough right now I'm at the ACCU conference with Marshall Clow (the force behind string_view et al on the committee) and I was quite literally about to ask him at the bar earlier today before I was called away about his views on string_view versus Bjarne's Guideline Support Library (GSL) gsl::span<T> which is a very similar thing (gsl-lite is my personal favourite implementation of the GSL as it's 03 compatible, but there are many others). I had heard they were to be unified into a single implementation for standardisation, and the gsl::span<T> direction is to be the future, but I'll report back here from the horse's mouth himself if I'm wrong on that. For now, assume the gsl::span<T> direction is the current future and Boost will get updated to have something similar soon, even if using string_view = gsl::span<char> is essentially string_view.

Edit: I just spoke to Marshall downstairs. He tells me that string_view, as per the implementation in Boost, is definitely in C++ 17. array_view is not, nor is anything historically surrounding string_view for now.

The GSL string_span is a separate entity not expected to enter in C++ 17, nor are there any present plans to unify the implementations as they solve different use cases, specifically that string_view is always a constant view of the borrowed character array, whereas string_span is expected to be a potentially modifiable view of the borrowed character array with potential uses as a source for construction of new strings, so string_span might perhaps eventually become a generalisation of string_view in some future C++ standard.

Niall Douglas
  • 8,293
  • 1
  • 40
  • 50
  • 1
    FYI the last update from the committee on string_view, array_view and span was in February and is detailed at http://stackoverflow.com/a/36207785/805579 – Niall Douglas Apr 19 '16 at 21:56
  • 18
    I am slightly confused about this being accepted answer, question was about `boost::string_ref` and `boost::string_view` and yet this answer seems to be about `gsl::` `boost::` and `std:: c++17` ? – Aleksander Fular Apr 03 '18 at 09:57
7

According to this email from the boost mailing list, boost::string_ref won't be used in the future and is being replaced by string_view in other boost libraries.

boost::string_view has the following advantages:

  • Better matches what the standards committee is doing for C++17
  • Has WAY more constexpr support
leecbaker
  • 3,283
  • 2
  • 31
  • 48
  • 6
    "Better matches what the standards committee is doing for C++17" - what, specifically, does that mean (in differences to boost::string_ref)? – sehe Apr 18 '16 at 23:08