Watch for dangling std::string_view

Question | Oct 19, 2019 | nwheeler 

An std::string_view object provides a read-only std::string form of interface to a contiguous sequence or array of chars. Typically, an std::string_view object contains a pointer to constant char and a size members. The pointer points to the beginning of the array, and the size has its length. Here are some elementary examples of std::string_view:

void f(std::string_view sv) {
 std::cout << sv << "\n";
}

std::string s;

// std::string to std::string_view
f(s); 

//literal to std::string_view
f("Hello"); 

// std::string_view::substr returns a std::string_view
f(std::string_view("Hello World").substr(0,5)); 

As std::string_view merely refers to a char sequence, we must ensure that the sequence or array should at least live for the lifetime of the std::string_view. An std::string_view that outlives its array is akin to a dangling pointer, which must be avoided. Most of the bugs associated with dangling std::string_view arise because of an std::string_view inadvertently refers to a char sequence in a temporary std::string object, and we need to be watchful for those typical patterns. These are some examples of dangling std:string_view:

// void f(std::string_view sv);

f("Hello"s); // Bad! std::string is temporary here

std::string_view g(const std::string& s) {
  //Bad! substr returned std::string is destroyed
  return s.substr(0,4); 
}

Below are some use cases of std::string_view. You have to identify which of these would result in a dangling std::string_view object:

A

std::string_view foo(const std::vector<std::string>& v) {
  // v.front() returns const std::string&
  return v.size() ? v.front() : "";  
}

B

std::string s("Hello");
std::string_view sv = s + " World";    

C

std::string g(){ 
 return "Hello World"; 
}

auto s = g();
std::string_view sv = s;

D

std::string s("Hello World");
std::string_view sv = s.substr(6,5);

Select below all the cases from above which have dangling std::string_view. Note that the correct choices are those which have a dangling std::string_view condition. (Check the Explanations for details)

2015 nextptr