Did you see the "Spooky C++" post? https://www.nablag.com/spooky_cpp
Did you already know that? Do all your junior C++ programmers know that? Do you have lints turned on in every C++ project and dep for that?
Cause I had to really look at it to figure it out. And I had to think, "Would Rust allow this code?"
I mean, you could just turn warnings on: https://godbolt.org/z/ns6cb9T6q
But yes, I know that any function with the signature `std::string_view(const std::string&)` or especially `std::string_view(std::string)` is a land mine.
I asked a local LLM to do a code review on it. (gemma4:12b) It flagged the bug, with all caps "THIS WILL CAUSE UNDEFINED BEHAVIOR", and suggested a fix.
For some reason both c++ and rust decided to go with "lets make temporaries have insanely short lifetimes! Kill them immediatly at the first semicolon we can!" and I don't know why. (rust at least makes it a compiler error to misuse them, but still annoying.)
C has a much better design with its compound literals having block scope lifetimes.
I'm not sure what's so "spooky" about this example. It's really obvious if you know what `std::string_view` is. `Sanitize()` signature is fishy to begin with -- it takes a single string by const ref, but returns a string view. And I don't even consider myself very experienced in C++.
Sure, it'd be nice if the compiler didn't allow you to write this code. But there are worse examples than this out there.