logoalt Hacker News

kmeisthax • yesterday at 12:25 AM • 2 replies • view on HN

The main things that C++ has that Rust doesn't is inheritance[0] and move constructors[1]. In practice you won't notice either unless you're writing bindings to C++ code or UI toolkits. More importantly, neither feature imperils memory safety. Rust left those out for reasons of taste[2].

What actually imperils memory safety in C++ is primarily cultural, not technical. In Rust, we have a lot of both stdlib and third-party library code that is properly designed and works well. Borrowck and the unsafe keyword are the primary technical enforcement of Rust's safety requirements, both of which could be grafted onto C++, but it probably would not do nearly as well of a job in C++ as it does in Rust.

Take, for example, the C++ contracts feature. The reason why the ISO vote on C++26 failed is because the ISO spec for contracts specifically allows compilers that do not enforce them. This is absolutely, obviously stupid; but it is also fairly typical of C++ culture. C++, being derived from C, is plagued with the notion that the programmer is ultimately responsible for performance, even though optimizing compilers have been a thing for decades and it is genuinely difficult to find performance improvements just by shuffling lines of code around. That's why they would consider it acceptable to add a safety feature and allow it to be turned off.

In Rust, you can't turn off the borrow checker. No, not even in unsafe code. Unsafe lets you lie a little to the borrow checker and type system - you can construct references from raw pointers or transmute types. But you are ultimately still beholden to those rules and you'd better make sure everything you do lines up right regardless of what happens in safe Rust.

[0] Rust traits can do inheritance-like things, so bindings to object systems with inheritance (such as GObject) will use them a lot. This still, however, results in a lot of boilerplate needing to be written - at the very least, every subclassable type needs its own trait for override implementation, etc.

[1] In Rust, all types are movable at any time. This makes self-referential structs impossible to implement safely, which means linked lists are a pain and async is basically impossible to do soundly. Pin<T> implements immovable types as a special kind of smart pointer, which is good enough to make async sound (albeit at the cost of a lot of compiler magic). But in C++ all types manage their own movement: this would be the equivalent of having a Move trait that gets called every time a struct gets moved.

[2] Specifically, "composition is preferred over inheritance." Inheritance requires developers invent a taxonomy of parent types and much early C++ code was plagued with nonsensical taxonomies.


Replies

menaerus • today at 10:22 AM

I think that there is very little to no evidence that would suggest contracts provide substantial improvement wrt safety concerns. For that reason alone I would not call the decision of contracts being an opt-in feature obviously stupid because it is obviously not so obvious. With each feature that comes in, and with each different codebase, there's always a tradeoff to be made based on the judgment for given case and not due to general public wishful thinking. It goes the same with other similarly unsubstantiated things that C++ gets grilled for. ABI break for example.

pjmlp • yesterday at 8:52 AM

Also template programming with constexpr + concepts, and if C++26 ever makes it, static reflection, are much more usable than the way Rust macros work.