What is std::string_view, and what are the potential pitfalls of using it with temporary strings?
Last updated
std::string_viewis a non-owning, read-only pointer-plus-length view over characters owned elsewhere. It is cheap to pass and avoids copies.It doesn't extend lifetimes: viewing a temporary
std::string(or returning a view into a local) leaves it dangling, causing undefined behavior.It isn't guaranteed null-terminated, so don't pass
.data()to C APIs expecting a C-string.