std::string_view
std::string_view is a non-owning view of a contiguous sequence of char: a pointer and a length. It can refer to a std::string, a string literal, or a substring, without copying the characters.
Detailed Explanation
A function that only reads text used to take const std::string&. Callers with a literal wrote std::string("GET") just to bind the reference, which may allocate. Callers with a substring wrote s.substr(0, 3), which allocates again. std::string_view (C++17) is the type that means "I will look at some characters I do not own".
It is two words: a const char* and a size_t. sizeof(string_view) is 16 bytes on a 64-bit ABI. Copying a view copies those two words, not the text. remove_prefix, remove_suffix, and substr adjust the view in place or return a new view; they do not allocate.
Construction is implicit from const char* (which scans for '\0'), from const std::string&, and from a pointer plus a length. A string_view can contain interior zeros when built from a pointer and a count. data() is not necessarily null-terminated: a view into the middle of a string is not a C string. Do not pass sv.data() to an API that expects strlen without also passing sv.size(), or without copying into a string.
Lifetime is the whole game. The view does not keep the characters alive. A function that returns string_view into a local string dangles. A view into a temporary string dangles at the end of the full expression: string_view v = std::string("x"); is a dangling view. string_view v = "x"; is fine; the literal lives for the program.
Use string_view for read parameters, for parsing windows that walk through a buffer, and for storing a view only when you also store the owner next to it (or the owner is static). Do not use it as a stored member unless the class's contract says "this refers to memory you promise to keep". Do not use it as a return type from a function that built a string locally; return string.
Comparisons are lexicographic on unsigned bytes, like string. Hashing is supported so a string_view can look up in an unordered_map<string, ...> only if the map's hash/eq are transparent (std::hash<std::string> is not transparent by default in C++17; C++20 heterogeneous lookup needs extra types). Many codebases keep unordered_map<string, T> and construct a temporary string key, or they use a transparent hasher.
std::u8string_view, wstring_view, and friends exist for the other character types. The same lifetime rules apply.
Code Examples
#include <iostream>
#include <string>
#include <string_view>
void greet(std::string_view name) {
std::cout << "hi " << name << '\n';
}
int main() {
greet("ada"); // literal, no allocation
std::string who = "al";
greet(who); // view into the string
greet(std::string_view(who).substr(0, 2));
}#include <string>
#include <string_view>
std::string_view firstWord(std::string_view line) {
auto pos = line.find(' ');
return pos == std::string_view::npos ? line : line.substr(0, pos);
}
int main() {
std::string line = "GET /index";
auto verb = firstWord(line); // valid: line outlives verb
(void)verb;
// std::string_view bad = firstWord(std::string("x y")); // dangles
}Common Gotchas
Binding a view to a temporary `string`
`string_view v = s.substr(0, 3);` dangles because `substr` returns a `string`. Write `string v = s.substr(0, 3);` or `string_view v = string_view(s).substr(0, 3);`.
`data()` is not always null-terminated
`fopen(sv.data(), "r")` reads past the view. Copy to a `string` or use an API that takes a length.
Storing `string_view` in a struct
the struct does not own text. If the source `string` moves or dies, the member is dangling. Store `string` unless you have a documented arena.
Writing through a view is not possible
the characters are `const`. To mutate, own a `string` or use a `span<char>` over mutable memory.