Can std::string_view data() be pointer-compared across assignment and copy?

Viewed 180

If I make a copy of a non-empty string_view, via assignment or copy-construction, are the data() pointers of the two string_views guaranteed to compare equal?

For example:

  const char* base = "foo[blah]bar[blah]";
  std::string_view b1{base + 4, 4}; // points to first "blah"
  std::string_view b2{base + 13, 4}; // points to second "blah"
  assert(b1 == b2); // this is just string comparison
  assert(b1.data() != b2.data()); // different underlying data pointer

  std::string_view b3 = b1; // Create a copy of b1
  assert(b1.data() == b3.data()); // is this guaranteed to hold?
1 Answers
Related