13

I just ran into some misunderstanding: at least in libc++ implementation std::experimental::string_view has the following concise implementation:

template <class _CharT, class _Traits....>
class basic_string_view {
public:
   typedef _CharT value_type;
   ...
   template <class _Allocator>
   basic_string_view(const basic_string<_CharT, _Traits, _Allocator>& str):
       __data(str.data()), __size(str.size())
   {
   }

private:
   const value_type* __data;
   size_type __size;
};

Does this implementation imply that if we pass rvalue expression to this constructor, we will get undefined behaviour when using __data after construction?

TemplateRex
  • 65,583
  • 16
  • 147
  • 283
Bikineev
  • 1,645
  • 14
  • 20

1 Answers1

19

That's right. A string_view is a non-owning wrapper with reference semantics that must only be used when the referred string outlives the use of the view.

The typical use case is in function parameters where the actual string lives for the duration of the function call and the function body never stores the view, but only reads it:

void foo(std::experimental::string_view message)  // pass by value
{
    std::cout << "You said, '" << message << "'.\n";
}

Usage:

foo("Hello");       // OK, string literal has static storage
foo(s);             // OK, s is alive
foo(s.substr(1));   // OK, temporary lives until end of full-expression

The moral is: If you only need the string for the duration of the function body, give the function a string_view parameter, and it can uniformly bind to any kind of stringoid argument. You don't need a function template, copying string_views is cheap, and you get some neat substringing operations for free. By contrast, never store a string_view, but always store astring:

struct X
{
    X(std::experimental::string_view s) : s_(s) {}

    std::string s_;     // NEVER have a string_view class member!
};
Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
  • 3
    "Never" is a bit harsh but if you do store it then you need to take the same care you would take if storing a "char *" – plugwash Oct 20 '16 at 13:41
  • 1
    @plugwash: Maybe. But I'd say you'd definitely be going off-message if you stored the view. Yes, in a transient class that's only used as a prvalue you *might* be able to make use of this, but such code is probably better written with lambdas that make it clear that the viewed string outlives the callable. – Kerrek SB Oct 20 '16 at 14:18