What is std::string_view, and what are the potential pitfalls of using it with temporary strings?

Last updated 21 June 2026

  • std::string_view is 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.

  • https://abseil.io/tips/1
  • https://blog.csdn.net/zhangyifei216/category_9276341.html
  • https://wanghenshui.github.io/2019/08/05/abseil-totw.html
  • https://www.geeksforgeeks.org/cpp/class-stdstring_view-in-cpp-17/
All C++ interview questions