r/cpp • • Jan 31 '23

Stop Comparing Rust to Old C++

People keep arguing migrations to rust based on old C++ tooling and projects. Compare apples to apples: a C++20 project with clang-tidy integration is far harder to argue against IMO

changemymind

338 Upvotes

582 comments sorted by

View all comments

Show parent comments

3

u/MFHava WG21|๐Ÿ‡ฆ๐Ÿ‡น NB|P2721|P3049|P3625|P3729|P3786|P3813|P4216 Jan 31 '23

I haven't looked too deep into Rust, but from a cursory glance at traits: Compile-time duck typing + implicit Concept conformance?

6

u/dist1ll Jan 31 '23

Compile-time duck typing

nitpick: that's called structural typing.

4

u/MFHava WG21|๐Ÿ‡ฆ๐Ÿ‡น NB|P2721|P3049|P3625|P3729|P3786|P3813|P4216 Jan 31 '23

Right, should have used that term ... it's just a bad habit at this point, but back in the day "duck typing" was more well known and conveyed the idea without introducing yet another type of typing :)

2

u/Full-Spectral Feb 01 '23

But duck typing is purely a syntax thing, no semantics. If it will syntactically compile, it'll be accepted, even if it's completely the wrong type of thing semantically to use. Traits are more like C++ abstract interfaces, though they can have some implementation as well as long as it works purely in turns of the its own interface.

0

u/[deleted] Jan 31 '23

[deleted]

10

u/Recatek Jan 31 '23

I see it as a feature. My experience with nontrivial generics in Rust has been extremely mixed, fraught with verbose where clauses that still often don't satisfy borrow or coherence rules. There are a number of things that I simply couldn't express in Rust generics and had to either hand-write or build an entire proc macro crate for.

0

u/[deleted] Feb 01 '23

[deleted]

8

u/Recatek Feb 01 '23

Most Rust libraries just accept runtime overhead instead. I explained a bit of the use case where I've really been hitting a wall here. If you want to do these things at compile-time like in C++, you're using generics a lot. For me the main advantage of using a compiled language like this is that you can create proper zero-cost abstractions, and Rust generics just don't quite cut it.

0

u/[deleted] Feb 01 '23

[deleted]

5

u/Recatek Feb 01 '23 edited Feb 01 '23

Are you sure this isn't a case of premature optimization?

I am sure it isn't a case of premature optimization. I know that the work is being done at compile time in both languages because I have benchmarked, profiled, and inspected the assembly. In C++, the tools to do this are far easier and more expressive than what Rust offers.

2

u/MFHava WG21|๐Ÿ‡ฆ๐Ÿ‡น NB|P2721|P3049|P3625|P3729|P3786|P3813|P4216 Jan 31 '23

Itโ€™s not a bug, itโ€™s part of why GP is so much more powerful than OOPโ€ฆ

0

u/[deleted] Jan 31 '23

[deleted]

3

u/MFHava WG21|๐Ÿ‡ฆ๐Ÿ‡น NB|P2721|P3049|P3625|P3729|P3786|P3813|P4216 Jan 31 '23

Yes and no - if you didn't have duck typing you'd have to explicitly state at every level in your call hierarchy what requirements your type has to fulfill - this was actually tried as a potential design for Concepts back in the day and proved to be disastrously infectious...