r/cpp • • May 04 '22

C++ & Rust: Generics and Specialization

https://www.tangramvision.com/blog/c-rust-generics-and-specialization
0 Upvotes

20 comments sorted by

29

u/UnicycleBloke May 05 '22

Article's author appears to be unaware of type traits, static assertions and concepts in C++. Nuff said.

13

u/CornedBee May 05 '22

I thought the same, but there is a short info box that mentions these things. Article doesn't go into details, but correctly points out that all of these features do not move template definition checking to definition time.

8

u/ioctl79 May 05 '22

None of those things provide the ability to check template definitions for soundness before instantiation. All of them are applied only once you try to actually substitute parameters into the template. In contrast, the Rust scheme (as described in the article -- I don't know Rust) only permits the body of the generic to use the operations specified in the constraints.

12

u/UnicycleBloke May 05 '22

One way or another, the code will not compile. I felt the article would be improved by a discussion of how constraints can be added in C++, even if they are optional.

6

u/ioctl79 May 05 '22

The problem is that it will compile, and even work until you use it with a type that doesn't implement some behavior or semantics that you were implicitly depending on, and then you get to sift through 10kb of template errors from the bowels of the STL. Whether getting validation up front is worth the loss of power is debatable, but it is certainly not something that C++ provides.

3

u/D_0b May 05 '22

it is a tradeoff. C++ concepts can do the job, but it is on the programmer to only use what is specified in the concept. On the other hand in Rust you can't mix 2 unrelated generic libraries that define their own traits and classes even if they would otherwise satisfy the same concepts in C++. (unless something changed since I last read about it??)

1

u/semtexzv Jul 12 '22

Are these 'concepts' in a room with us now?

1

u/D_0b Jul 12 '22

point being you can always go from less constrained to more constrained, but not the other way around.

1

u/semtexzv Jul 12 '22

My point is, I have never seen concepts in the wild.

1

u/D_0b Jul 12 '22

practically every C++20 project/library is using concepts.

from msvc STL, any ranges libs, most of coroutine libs, the new libunifex...

4

u/[deleted] May 05 '22

It's more generic way, personally when i don't want code to compile by specific type i add static_assert, i want my code to valid for i supposed to use, not what are all allowed types to satisfy the rules. That 2 different manners.

2

u/lak16 May 05 '22

That's what concepts are for.

3

u/ioctl79 May 05 '22

No, it's really not. Concepts let you limit what kind of types your template will accept, but satisfying the concepts is not sufficient to determine if a given template will compile with specific arguments.

https://godbolt.org/z/rMhGadj95

1

u/Nobody_1707 May 06 '22

This is, sadly, an inherent limitation of template instantiation. I don't believe it would even be possible to implement this kind of checking in C++ without reflection/metaclasses.

10

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

u/[deleted] 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

u/[deleted] 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

  1. Borrows the pointer - which is actually limitation, but that's well define program execution

  2. Extend object lifetime as keyword - but if you ever maintain a bash script you will know ' is a crap symbol.

3

u/[deleted] 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.