Remix.run Logo
▲ apples_oranges 6 hours ago

C++ dev here. I love this mess of a language ..

▲01HNNWZ0MV43FF 6 hours ago | parent | next [-]

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?"

▲William_BB 6 hours ago | parent | next [-]

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.

▲grg0 3 hours ago | parent | next [-]

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.

▲simonask 4 hours ago | parent | prev [-]

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 [-]

I think it is a real issue, yet in some ways worse in Rust. Mojo likely does this way better than both Rust and C++.

An addition could be made to C++ to make this less prone to errors, but Rust cultists and Jewish pedophiles make that harder to fix, with Jewish pedophiles being entrenched in both Rust and C++ political positions as well as in Ycombinator (Epstein board member).

▲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...

▲nwallin 3 hours ago | parent | prev | next [-]

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.

▲cv5005 4 hours ago | parent | prev [-]

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.

▲lkjdsklf 6 hours ago | parent | prev | next [-]

It’s a weird language

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

▲bernds74 6 hours ago | parent [-]

This roughly sums up my feelings. C++ is horrendous, but it's also the best tool I have.

▲coldpie 6 hours ago | parent | prev | next [-]

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.

▲cogman10 6 hours ago | parent | next [-]

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.

▲tcfhgj 6 hours ago | parent | next [-]

> Here's the thing. I'd say that C++ is a great language for getting things done.

my productivity sky rocketed when I switched to Rust.

▲cogman10 6 hours ago | parent | next [-]

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.

▲tcfhgj 5 hours ago | parent [-]

> 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.

▲cogman10 3 hours ago | parent [-]

> 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.

Well, that's the thing about rust and async. It's far more restrictive (necessarily) than the bounds of safety. The hard problem rust runs into is that async functions can be executed outside of lexical and the lifetime tracking of the rust language. To be absolutely safe, rust ends up needing to assume that async can be executed with no respect to the surrounding lifetimes.

Rust would really benefit from something like structured concurrency in terms of the lifetimes of async functions.

▲tcfhgj an hour ago | parent [-]

> Well, that's the thing about rust and async. It's far more restrictive (necessarily) than the bounds of safety.

I obviously was not talking about things illegal in safe Rust, which are actually safe, e.g. you cannot send types which are not allowed to be moved across threads across threads in C++ either.

> To be absolutely safe, rust ends up needing to assume that async can be executed with no respect to the surrounding lifetimes.

seems to work with surrounding life times?

https://play.rust-lang.org/?version=stable&mode=debug&editio...

▲pjmlp 6 hours ago | parent | prev [-]

Game developers can't say the same though.

▲cozzyd 4 hours ago | parent | prev [-]

Yes, C++ is an omnibus language where all things are possible.

▲simonask 4 hours ago | parent [-]

All things... except non-nullable pointers, movable values, type punning, ergonomic sum types, and the list is frankly much longer.

The only way you could think that C++ lets you do everything is if you've never tried anything else.

▲materielle 6 hours ago | parent | prev | next [-]

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.

▲coldpie 6 hours ago | parent [-]

> 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.

▲pjmlp 6 hours ago | parent | prev | next [-]

Nope, I like C++ because it was already Typescript for C in 1993, coming from Turbo Pascal.

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.

▲trealira 4 hours ago | parent | prev [-]

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.

▲gchamonlive 6 hours ago | parent | prev | next [-]

Stockholm syndrome we're all vulnerable to. I love bash for instance.

▲raincole 6 hours ago | parent | prev [-]

Yes, people even have a term for this. It's named after a Swedish city.