How to Use string_view in C++ - Basics, Benefits, and Best Practices
Talkat CppCon 2024 · on Tuesday, 2024-09-17· at 15:15· Cottonwood 8/9, 6700 North Gaylord Rockies Boulevard·
summary
string_view is a lightweight pointer+length view that avoids heap allocation for many APIs but string_view is pointer+length and not null terminated so C functions and implicit conversions bite. C++17/20/23 add useful constructors and the ""sv literal skips strlen but permits embedded NULs. Use std::span for contiguous containers (std::span for contiguous containers), prefer passing by value, and avoid returning views into temporaries. Add a transparent comparator for map<string,int> lookups from string_view and enable clang/MSVC dangling/lifetime warnings; case study swaps vector<string> for span<string_view> and removes runtime allocations.
AI-written summary · about
chapters (11)
- 0:00Intro
- 1:30Motivation
- 14:20Stringview vs string
- 17:33Stringview interoperability
- 25:13Problems with stringview
- 30:00Stringview from templates
- 47:20Case Study
- 54:32Wrap Up
- 55:52Special Thanks
- 56:25Questions Comments
- 58:22Questions Answers
related talks
01h 00mEnough string_view to hang ourselves
56mC++17: The Library Features
57mA Short Life span - For A Regular Mess
01h 36mC++ Standard Views
01h 03mC++17 part 2: The Library Features
52mC++ Performance Tips - Cutting Down on Unnecessary Objects
01h 00mThe Guideline Support Library: One Year Later
01h 30mTaming C++23 - The Things About C++23 You Do Not Know
