| ▲ | simonask 4 hours ago | |||||||
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. | ||||||||
| ▲ | William_BB 2 hours ago | parent | next [-] | |||||||
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. | ||||||||
| ||||||||
| ▲ | lollollol3 2 hours ago | parent | prev [-] | |||||||
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. 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... | ||||||||