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.
You have to know a lot about C++ to tell at a glance what's wrong with this code. I fall in that category, and I can tell you I think that the signature of Sanitize() looks extremely reasonable!
What's completely unreasonable here is how easy it is for code that looks reasonable in C++ to be deeply dangerous. This is a perfectly normal piece of code, and it looks like the author is even conscientious about nice things like avoiding too many string copies. We shouldn't be out here defending the absolutely unhinged semantics that led to this.
C++ is so extremely hostile that people who try to write good code end up with a very defensive style of programming if they know what they're doing. In my experience, this tends to either almost fully eradicate any performance benefits they were chasing by choosing C++ in the first place, or blow up the time it takes to deliver the product.
I agree with your general sentiment, but imo this particular example is oversold. Maybe I'm wrong.
The difference between const ref string and string view is one of the first things you learn when you learn about `string view`.
To me, the signature of `Sanitize` is fishy because it's implied that the returned string view is of the const ref string argument. But I agree there are more than one ways to break it.
What is funny is that Rust has similar or worse issues regarding temporary lifetimes. Only some UB is caught (and in practice only sometimes), issues like deadlocks aren't.
Challenge to you, simonask: Explain why the curly braces here determines whether there is a deadlock or not. Bonus points if you do so without asking for help or looking anything up.
I'm responding so future readers see the answer, because oooh boy am I not engaging with this account.
The curly braces shortens the lifespan of the `MutexGuard` so it gets unlocked before the body of the if-statement. Normally, the temporary used in the if-let pattern lives until the end of the if-statement.
This wouldn't compile if the value guarded by the Mutex wasn't trivially copyable, but it is.
(Note how none of this is UB, just a particularly contrived way to introduce a deadlock.)
I don't think that is a fair assessment. Imagine Sanitize() and its caller are defined in different files, which is a more realistic scenario. On the one hand, you look at Sanitize's signature and it is "fine" (I agree a string_view argument would be better); it is implicitly stating that the caller is responsible for the lifetime of the argument. Separately, you look at the call and it has a good chance of passing inadvertently. Marking std::string's constructor explicit would have been a slight improvement but it still doesn't solve the problem. Overall, the fact that this code is possible and yields different results under different optimization levels is just cooked. As a programmer, I want to focus on solving problems, not on keeping my guard up at 110% and working around bad language design choices.
To reply to GP: many projects specifically avoid the STL.
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.
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.
I have a theory that the reason people like C++ is because it's challenging to use. It's like a puzzle or a riddle or a Dark Souls game. It feels good to get something done in C++ because there's so, so many ways for it to go wrong that when you do it right(-ish...), it feels like you're a master of a complex skill. This makes it a terrible language for actually getting things done, of course, but people like that feeling of overcoming the challenge, so they keep using it.
Here's the thing. I'd say that C++ is a great language for getting things done.
It gives you one of those fat swiss army knives I had as a kid with a spoon, fork, knife, toothpick, saw, nail trimmer, etc. The problem is it's easy to get dirty and the spoon would get locked in place after one use. But it was there, and it worked (once).
C++ is extremely malleable. I think the reason people like it is very similar to the reason people like lisp. That ability to do horrors like make a lisp interpreter with macros or templates is the charm of C++.
And if you are the only one writing it, it can be a joy because there are few limits to what you can or can't do.
It's only when you have to share a codebase with others that things can get out of hand fast without some committee established usage code and pledge.
Probably, particularly in terms of writing correct code.
But C++ allows me to really easily write multithreaded code which I can convince myself is safe :) (and most of the time, is correct, just not on the edge cases). Rust makes that painful.
> Probably, particularly in terms of writing correct code.
package management, build system, compiler errors, language simplicity (yes, it adds life times, but removes so many other things), Rust macros, language ergonomics (e.g. match, macros, enums, async, if let, etc.), not having to think about certain things going wrong (including not debugging why things went wrong)
> Rust makes [multithreaded code] painful.
Andreas Kling is switching away from C++ to Rust in Ladybird, because of exactly the reversed experience.
I have been using asynchronous multithreading via tokio - something I never dared to in C++.
Alone from the compiler errors I have got doing that, I would assume that I would have been dealing with an unbearable amount of debugging of subtle memory issues.
It definitely has to be more complicated than that. I know the stereotype is that the C++ language lawyers are up in their ivory towers and don’t “get stuff done” like us pragmatic JavaScript or Python developers.
But the reality is a ton of domains like graphics, video games, and cloud infrastructure is built on C++.
I think the Spanner and Chrome devs “got stuff done” for instance.
I could go on a tangent about the whole “maker vs coder” debate that has sprung up around AI. It feels similar to the C++ idea you’re talking about. The idea is that people who actually understand code and computer science concepts deeply are too busy yak-shaving to be productive like us “makers”.
That ivory tower type definitely exists.
But this false dichotomy seems to be defense mechanism more than anything.
There are smart people that are basically better at everything. They understand C++ deeply, but can code in Python too, build massive pieces of infrastructure like Spanner, and now they are better at driving AI, too.
Not that I’m defending any of the horrible language design decisions of C++. But, you see what I’m saying. People who have skills beyond is in one area aren’t necessarily deficient in another area. Maybe they are just smarter and more skilled than us in a ton of ways.
> But the reality is a ton of domains like graphics, video games, and cloud infrastructure is built on C++.
Yeah, legacy reasons. C++ was a decent-ish improvement over C in the early 90s when many of those APIs were being built out. It's the core language for the Windows API. We didn't get a decent, well supported replacement for C++ until like ten years ago. There's a lotttttttt of legacy C++ code that will stay in use for decades more.
I never said people can't be productive in C++, I'm saying the reason they like it is because it's so hard to use. Just like people can get really good at Dark Souls, people can get really good at C++, too, and they have fun doing it. But I still think that's a con of the language, you can get the same or even better code quality with many modern languages with a much lower bar to mastery.
IDK, I just like C++. I don't think that's the reason. It doesn't feel that good to get something done in C++, and it doesn't feel like a puzzle; it just feels mostly straightforward. The language which is actually like a puzzle and which feels good to write is Haskell, in my opinion, but it's not always as easy to get stuff done in Haskell. I suppose this is why many Haskell coders moved to Rust.
> This makes it a terrible language for actually getting things done,
The thing that I like about C++ the most is that it's best language for actually getting things done.
The 'problem' with C++ is that people who want to get things done go to the committee and say, "hey, I need to actually get things done, if you add this language facility, I can actually get things done better," and they'll often just say "ok" and add it to the language. And if you're the sort of person who believes you must use every single facility provided by the language, then you walk away from C++ thinking it's too complicated. But if you're the sort of programmer who's only interested in getting things done, you can simply ignore all that noise and just actually get things done with the facilities that you know and like.
You don't have to use deeply nested templates, complex dynamic inheritance trees, move semantics, Turing-complete template metaprocessing, you don't even have to manually manage your memory if you don't want to. (personally I avoid all of those things except move semantics) But it's there if you need it.
Replies (5)
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?"
Replies (3)
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.
Replies (2)
What's completely unreasonable here is how easy it is for code that looks reasonable in C++ to be deeply dangerous. This is a perfectly normal piece of code, and it looks like the author is even conscientious about nice things like avoiding too many string copies. We shouldn't be out here defending the absolutely unhinged semantics that led to this.
C++ is so extremely hostile that people who try to write good code end up with a very defensive style of programming if they know what they're doing. In my experience, this tends to either almost fully eradicate any performance benefits they were chasing by choosing C++ in the first place, or blow up the time it takes to deliver the product.
Replies (2)
The difference between const ref string and string view is one of the first things you learn when you learn about `string view`.
To me, the signature of `Sanitize` is fishy because it's implied that the returned string view is of the const ref string argument. But I agree there are more than one ways to break it.
https://fasterthanli.me/articles/a-rust-match-made-in-hell
Challenge to you, simonask: Explain why the curly braces here determines whether there is a deadlock or not. Bonus points if you do so without asking for help or looking anything up.
https://play.rust-lang.org/?version=stable&mode=debug&editio...
https://play.rust-lang.org/?version=stable&mode=debug&editio...
Replies (1)
The curly braces shortens the lifespan of the `MutexGuard` so it gets unlocked before the body of the if-statement. Normally, the temporary used in the if-let pattern lives until the end of the if-statement.
This wouldn't compile if the value guarded by the Mutex wasn't trivially copyable, but it is.
(Note how none of this is UB, just a particularly contrived way to introduce a deadlock.)
To reply to GP: many projects specifically avoid the STL.
C has a much better design with its compound literals having block scope lifetimes.
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.
Replies (5)
It gives you one of those fat swiss army knives I had as a kid with a spoon, fork, knife, toothpick, saw, nail trimmer, etc. The problem is it's easy to get dirty and the spoon would get locked in place after one use. But it was there, and it worked (once).
C++ is extremely malleable. I think the reason people like it is very similar to the reason people like lisp. That ability to do horrors like make a lisp interpreter with macros or templates is the charm of C++.
And if you are the only one writing it, it can be a joy because there are few limits to what you can or can't do.
It's only when you have to share a codebase with others that things can get out of hand fast without some committee established usage code and pledge.
Replies (2)
my productivity sky rocketed when I switched to Rust.
Replies (2)
But C++ allows me to really easily write multithreaded code which I can convince myself is safe :) (and most of the time, is correct, just not on the edge cases). Rust makes that painful.
Replies (1)
package management, build system, compiler errors, language simplicity (yes, it adds life times, but removes so many other things), Rust macros, language ergonomics (e.g. match, macros, enums, async, if let, etc.), not having to think about certain things going wrong (including not debugging why things went wrong)
> Rust makes [multithreaded code] painful.
Andreas Kling is switching away from C++ to Rust in Ladybird, because of exactly the reversed experience.
I have been using asynchronous multithreading via tokio - something I never dared to in C++. Alone from the compiler errors I have got doing that, I would assume that I would have been dealing with an unbearable amount of debugging of subtle memory issues.
Continue this thread (1 replies) →
Replies (1)
The only way you could think that C++ lets you do everything is if you've never tried anything else.
But the reality is a ton of domains like graphics, video games, and cloud infrastructure is built on C++.
I think the Spanner and Chrome devs “got stuff done” for instance.
I could go on a tangent about the whole “maker vs coder” debate that has sprung up around AI. It feels similar to the C++ idea you’re talking about. The idea is that people who actually understand code and computer science concepts deeply are too busy yak-shaving to be productive like us “makers”.
That ivory tower type definitely exists.
But this false dichotomy seems to be defense mechanism more than anything.
There are smart people that are basically better at everything. They understand C++ deeply, but can code in Python too, build massive pieces of infrastructure like Spanner, and now they are better at driving AI, too.
Not that I’m defending any of the horrible language design decisions of C++. But, you see what I’m saying. People who have skills beyond is in one area aren’t necessarily deficient in another area. Maybe they are just smarter and more skilled than us in a ton of ways.
Replies (1)
Yeah, legacy reasons. C++ was a decent-ish improvement over C in the early 90s when many of those APIs were being built out. It's the core language for the Windows API. We didn't get a decent, well supported replacement for C++ until like ten years ago. There's a lotttttttt of legacy C++ code that will stay in use for decades more.
I never said people can't be productive in C++, I'm saying the reason they like it is because it's so hard to use. Just like people can get really good at Dark Souls, people can get really good at C++, too, and they have fun doing it. But I still think that's a con of the language, you can get the same or even better code quality with many modern languages with a much lower bar to mastery.
Followed by the great developer experience of using Turbo Pascal alongside Turbo C++, later Delphi and C++ Builder on RAD Studio.
An experience that many languages still fail to reproduce nowadays, with exception of Java and .NET tooling.
You don't need to master every single feature.
The thing that I like about C++ the most is that it's best language for actually getting things done.
The 'problem' with C++ is that people who want to get things done go to the committee and say, "hey, I need to actually get things done, if you add this language facility, I can actually get things done better," and they'll often just say "ok" and add it to the language. And if you're the sort of person who believes you must use every single facility provided by the language, then you walk away from C++ thinking it's too complicated. But if you're the sort of programmer who's only interested in getting things done, you can simply ignore all that noise and just actually get things done with the facilities that you know and like.
You don't have to use deeply nested templates, complex dynamic inheritance trees, move semantics, Turing-complete template metaprocessing, you don't even have to manually manage your memory if you don't want to. (personally I avoid all of those things except move semantics) But it's there if you need it.
On your initial look, it looks like a total nightmare.
The more you learn and use it, you learn that it is even worse than you ever imagined
So you try something else and find yourself in a constant loop of “if this were c++ i could just go xyz, by nooo”
So then you love it
Replies (1)