Baffour Kusi Frimpong’s Post

🚫 Stop passing 𝒄𝒐𝒏𝒔𝒕 𝒔𝒕𝒅::𝒔𝒕𝒓𝒊𝒏𝒈& when you don't need to. There's a better way, and it's been in C++17 since 2017. Meet 𝒔𝒕𝒅::𝒔𝒕𝒓𝒊𝒏𝒈_𝒗𝒊𝒆𝒘. Here's a classic mistake I see in C++ codebases. When you pass "hello" to a 𝒄𝒐𝒏𝒔𝒕 𝒔𝒕𝒅::𝒔𝒕𝒓𝒊𝒏𝒈&, the runtime silently allocates a new 𝒔𝒕𝒅::𝒔𝒕𝒓𝒊𝒏𝒈. With 𝒔𝒕𝒓𝒊𝒏𝒈_𝒗𝒊𝒆𝒘? Nothing. Zero overhead. It's just a pointer + length pair pointing to an existing buffer. Where it really shines is substring parsing, no new, no heap, no copy. You're just sliding a window over existing memory. 🧠 Key insight: 𝒔𝒕𝒓𝒊𝒏𝒈_𝒗𝒊𝒆𝒘 is perfect for read-only string processing parsers, tokenizers, log analyzers, and protocol handlers. Anywhere you're slicing and inspecting strings without modifying them. Two caveats worth knowing: · Don't store a 𝒔𝒕𝒓𝒊𝒏𝒈_𝒗𝒊𝒆𝒘 if the underlying string can be destroyed, dangling reference territory. · It's non-owning by design. That's the whole point. Available since C++17. If you're not using it, you're leaving free performance on the table. This is Day 1 of my C++ deep-dive series. Follow along if you want to write faster, leaner C++. What's your go-to use case for string_view? Drop it below 👇 #cpp #cplusplus #programming #softwareengineering #performanceprogramming

  • text

Not a single word about ownership?

Why would runtime silently allocate a new string for const string& allocate ? Am I not seeing something ?

Like
Reply

Good stuff. But a question if there is ZERO overhead, where is the pointer and the length stored?

Like
Reply

It's a reference. The string is not copying in the process function

Oh my, it depends from so many things... And thats in general one of the bigger problems of C++. To many ways to do same things. I guess you know something about implemented "short string optimization" in different compilers sometimes you get different behaviours, i want to say it (cannot/yes/perhaps) depends on length of the string too. And if you call with string literals it changed everything too. Sometimes i even go back to "char *". One thousand ways to call a method or function with just a string ... and sometimes i working on legacy code bases which where i can/have only use old compiler g++ 3.4 because nothing else is available or even older. And there is nobody who transform it in more modern c++ where i have string_views!!! Thats another big problem, huge legacy code bases from the nineties we have still to maintain. And bytheway if i look in C++ standard library these days i really do not understand anything anymore and im an ordinary C++ developer since 1990 now. Why, why complexity had explode since C++11 so much?

Sure, string_view has its place, especially since , but let's be frank - string literals should be the exception rather than the norm outside of logging messages. const std::string& is perfectly valid for most use cases, and oftentimes, there's reasons to keep the string mutable in the first place (think about JSON deserialization).

Would there be anything stopping a good optimizing compiler from creating a static std::string object from "hello" and passing the address of that object, eliminating all runtime overhead?

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories