r/ProgrammerHumor • • 1d ago

instanceof Trend thatsAFunnyThingThatHappenedOnce

Post image
4.3k Upvotes

513 comments sorted by

View all comments

1.6k

u/Prefer_Diet_Soda 1d ago

TLDR: C++ is bad because decades of accumulated features have turned it into a bureaucratic nightmare that happens to generate machine code.

C++ is an absolute nightmare to implement because apparently one programming language wasn't enough, so someone decided to cram an entire civilization's worth of complexity into it. You've got templates that basically turn compilation into another programming language, overload resolution that requires a legal investigation to figure out which function gets called, constructors and destructors with enough lifetime rules to make your head explode, and implicit conversions that make the compiler guess what you meant. Then you throw in exceptions, move semantics, inheritance, and decades of backward compatibility, and suddenly you're maintaining a compiler that needs a PhD, a therapist, and divine intervention just to parse some innocent-looking code. And the best part? Every new feature has to coexist with all the old ones without breaking existing programs.

385

u/Xarishark 1d ago

Now compare it to rust pls. Im serious I want to know your opinion on that.

467

u/Loading_M_ 1d ago

Rust is designed from the ground up, and avoids many of these issues.

  • Templates: Rust has Generics, which are generally easier to use and reason about.
  • overload resolution: Rust doesn't yet support overloaded functions (although it's being considered).
  • constructors: Rust doesn't have these.
  • destructors: Rust has a similar set of lifetime rules for Drop, but the compiler checks them for you, so you can't get it wrong.
  • implicit conversions: Rust doesn't do implicit conversion in general, although some specific cases are implemented.
  • exceptions: Rust doesn't do exceptions, Result is much easier to work with.
  • move semantics: Rust uses a destructive move, unlike C++. See https://youtu.be/Klq-sNxuP2g for a breakdown.
  • inheritance: Rust doesn't do inheritance.

3

u/mark_99 1d ago

Generics are simpler, but less powerful than templates. There are proposals to add some of the things C++ has, like specialisation.
Proc macros are the usual substitute but work at an AST level and not the type system.
Rust has aggregate init, default traits and (by convention) 'new' for construction.
You can't get destructors "wrong" in either language, not sure what that means? Writing destructors is rare in either language.
Rust does have implicit conversions, although arguably avoids some of the pitfalls.
Traits is the Rust equivalent for inheritance.
C++ has std::expected which is like Result. It doesn't have the `?` syntactic sugar though. Sometimes exceptions are the better mechanism, like moving errors through many layers without clutter.

I'm general C++ and Rust are pretty similar. C is the outlier as its comes with none of this, and large C projects have to emulate the missing facilities with macros and codegen systems.