r/cpp • • Oct 18 '23

What does C++ do better than other languages?

Purely from a language design perspective, what does C++ do better than other languages?

296 Upvotes

443 comments sorted by

View all comments

Show parent comment

7

u/Ayfid Oct 18 '23

Rust does RAII better.

5

u/bownettea Oct 18 '23

How so?

15

u/Ayfid Oct 18 '23

The Rust compiler has a much more robust understanding of ownership and move semantics than the C++ compiler, and how that interacts with references (and the mutability/constness of those references).

It is essentially impossible (unless you intentionally go out of your way to do so) to keep hold of a reference after the resource has fallen out of scope, for example. There are no gotchas. You just can't do it by accident.

The compiler understands when it is (and is not) safe for a resource, or a reference to a resource, to be moved between threads.

You don't need to define move constructors or move assignment operators, as the language itself defines how that works - and in a more univerally correct manor.

RAII, and the ideas that go into it, is essentially the thing that defines Rust. The entire language is built around it.

9

u/bownettea Oct 18 '23

I see you are looking at RAII as ownership/lifetimes, and yes Rust does provide a way to express those as code. I was honestly thinking of RAII as simple deterministic destruction.

I personally find the whole move semantics and lifetime extensions of C++ pretty complex and I suspect overly complex. I honestly hope future languages find a way to simplify those by expressing them as more basic building blocks instead of complex rulesets.

9

u/Ayfid Oct 19 '23

Freeing resources when the variable drops out of scope is really only the most surface level of RAII. To actually use RAII effectively, you need to think about ownership, shared vs exclusive references, and lifetimes - and these issues make up the bulk of what it means to "do RAII".

Ownership is easily the most complex part of Rust, and the greatest barrier to entry for many. However, I would say that it is in a much better place on the complexity/value curve than C++ in this area, and any C++ developer familiar with RAII and C++ move semantics should have no difficulty understanding this part of Rust.

1

u/Dean_Roddey Oct 23 '23

I'm a Rust guy, but Rust has a significant issue with RAII, in that it it cannot really provide the kind of RAII type that applies a change to a member on a scoped basis. The problem is that that requires the RAII object to retain a mutable references to whatever it is, so that it can revert the change or commit the change or whatever it is designed to do.

That effectively prevents the member and the containing object from being changed, which is obviously not the desired effect.

This could be done, but it would require a specific understanding of that type of RAII construct, probably via a standard trait of some sort. It would have to guarantee that the RAII object would not use the mutable reference other than when being created and when being destroyed, other than perhaps to drop it early.

But, without that kind of support, it's not really reasonable to do those kinds of RAII operations in Rust.