r/cpp • u/ThatGeoGuy • May 04 '22
C++ & Rust: Generics and Specialization
https://www.tangramvision.com/blog/c-rust-generics-and-specialization10
u/Full-Spectral May 05 '22 edited May 05 '22
The short version of the Rust vs C++ thing that most C++ people would be surprised by, or that I was, because I just never thought about it much in C++ land, is that so much of what people are doing with C++ templates depends fundamentally on duck typing. That's both good and bad. It's bad because that's why we get retarded error output when something goes wrong, and it can probably allow for some things that should not happen to happen.
But, it's useful because it means that the things we pass to a template can be completely unrelated. In Rust you can't do that. The generic has to be defined in terms of an interface that anything it's instantiated for will share. That's good and bad for the opposite reasons. It's good because the generic can be verified at the point it's defined, you don't get the phone book if there's an error, and it's almost certainly much safer.
But, OTOH, you can't pass it random, unrelated stuff, which can be useful. For instance, I don't think you could create a Rust generic that operates on any enum that defines particular magic values, whereas you can in C++ and I do quite a bit.
3
u/Rusky May 06 '22
There is one more detail that lets Rust work with unrelated types in more situations than you might expect: Traits are decoupled from types, so the type author doesn't have to be the one to implement them.
This isn't a complete free-for-all though- there are some rules in place that prevent conflicting trait implementations from popping up in unrelated parts of the crate dependency graph. You can only implement a trait if either the trait or some part of the type (approximately) is defined in the same crate.
In practice this means that there are a few common patterns:
The familiar approach, where one crate (e.g. the standard library) defines the trait and then other crates define their own types and implement the trait for them.
The opposite direction, where one crate (again e.g. the standard library) defines the type and then other crates define their own traits and implement them for the type.
A sort of middle ground, like the familiar approach but with the trait implementations made optional, so you can depend on the crate that defines the type without pulling in the crate that defines the trait. In principle these could go in their own crates, but that would make it possible for a dependency graph to contain more than one without any individual crate being "at fault."
The common thread here is that traits become concrete vocabulary, much like the types you get from the standard library or libraries like Boost. The same basic sort of cross-codebase coordination happens in C++ already; Rust traits take that and add names and versions. For example your enum magic value example would involve the enum author not only knowing to define the magic values but also explicitly defining that they are doing it on purpose.
1
May 07 '22
I find myself doing that when working on spaghetti code with inheritance abuse.. We ended up with 2 classes defining the same 2 methods. I needed one method to operate on both. I prefer the go solution, where you define some private interface with these two shared methods then just accept that into the function. Any classes implementing those two methods will be able to act as if they have the private interface. Having said that i did it with a template but of course that added to the binary size.
-1
May 06 '22
. It's good because the generic can be verified at the point it's defined,
Actually doesn't need at all. The point of generic is to reuse same logic over desired types, desired types != all types that satisfy a trait. Actually Rust do only 2 things right
Borrows the pointer - which is actually limitation, but that's well define program execution
Extend object lifetime as keyword - but if you ever maintain a bash script you will know ' is a crap symbol.
3
May 05 '22
Disagree of conclusion, validity of traits proof nothing, minimal allowed set of traits is better than all allowed, since you care about something hypothetically. That makes code verbose.
29
u/UnicycleBloke May 05 '22
Article's author appears to be unaware of type traits, static assertions and concepts in C++. Nuff said.