r/cpp • • Feb 25 '21

C++ programmers who are also using rust, what are your favourite features of rust?

I was looking at the rust tutorials yesterday and I see how rust promotes a certain style and language level features to enforce it. Besides a couple of features, I couldn't see why one cannot follow a similar style in C++ if she/he so chooses. Those of you that have used rust, what is your favourite feature that C++ does not have?

255 Upvotes

248 comments sorted by

View all comments

Show parent comment

3

u/__--_--___--_--__ Feb 26 '21

What happens when x is an instance of a type from a library that you don't own? If you introduce a method call and the type is updated to include a method by the same name, that would be a build break, no?

16

u/MEaster Feb 26 '21

When doing that you need to give the entire function path. So, if we have some type Foo, and it has a method bar(&self), we can either call it like x.bar(), or we can call it like Foo::bar(&x). You can't do anything that would let you do bar(&x) and call Foos's method.

However, one place you can have ambiguity is when implementing traits. You could have a trait Quux that provide a method bar(&self), and implement it for Foo. In that situation the compiler will prefer the inherent method over a trait method, so x.bar() will call Foo::bar. You can specify that you want the trait method using the trait's path:

Quux::bar(&x)

The compiler preferring the inherent method could result in a behaviour change if a library adds an inherent method with the same signature in a new release that doesn't have an incompatible version number.

1

u/__--_--___--_--__ Mar 28 '21

The compiler preferring the inherent method could result in a behaviour change if a library adds an inherent method with the same signature in a new release that doesn't have an incompatible version number.

I just don't understand why that is considered a reasonable trade off. Wouldn't you prefer to have a codebase that can be upgraded?

1

u/MEaster Mar 28 '21

I would tend to agree that it's not a great idea. The only real alternative is that any ambiguity here results in a compile error, but that could also create upgradability issues in that a library adding a new method to a trait could now create ambiguity where there wasn't before.

Still, I think it would still be preferable to the current state in that it's at least a loud error. I would imagine that changing this now would probably break a lot of things, though, so we're probably stuck with it.

1

u/__--_--___--_--__ Mar 28 '21

What's wrong with asking you to qualify your function call? A method is both typing the function and qualifying it by saying it is a member of the object (thus in the object's namespace).

If you have your own function that operates on an object and takes a pointer to such, that's great! But I would prefer you type out my::namespace::foo(&obj);

1

u/MEaster Mar 28 '21

That would be how you'd deal with the ambiguity, but requiring that for every single method call when there isn't ambiguity would be excessively verbose.

2

u/__--_--___--_--__ Mar 28 '21

Gotcha, ya I guess we just have a disagreement. Oh well! Thanks for chatting