Comment by tialaramex
19 days ago
> then end up reimplementing virtual interfaces manually
C++ dynamic dispatch (your "virtual interfaces") is achieved by welding a vtable onto every type and providing a pointer to that vtable for instances of the type. If in 90% of your code you deal with specific types like Goose or Swan or Duck or Seagull, and only 10% needs to work with the broad Bird category well, too bad, every Goose, Swan, Duck and Seagull carries around that vtable pointer even if it goes nowhere near that 10% of the system. This way your Bird code "just works" in C++.
That's not the only way to crack this nut. Idiomatic Rust approach uses vtables only in the Bird code, elsewhere they don't exist, and thus don't take up space in a Duck or whatever that's always a Duck, but in exchange now you're spending more time thinking, because by default there aren't any vtables and so dynamic dispatch isn't possible at all.
So while that C programmer has to implement features by hand, they are at least able to specifically implement the feature they wanted, not whatever was easiest for Bjarne Stroustrup last century.
C++ nudges you to think in terms of single elements. Operator overloading, ctors/dtors, references, etc. you pay that cost all over the place.
C programs tend to nudge you into thinking in terms of arrays of data.
For game development you generally want to think this way. The cost of vtables and all the cache misses doesn’t have to be paid. A game has to stream bytes. Many things at once. Rarely single elements at a time.
Only if devs lack the skills to understand what to actually use.
There is this anti-C++ bias that keeps forgetting that using C++ doesn't mean use every feature.
Just like many keep using C as if C89 was latest, and never adopt anything added in the last 30 years.
I agree, there is a nice language buried in all the features trying to escape.
The struggle is that if you choose a subset of the language to use and you have dependencies… well you’re including whatever features they use into your subset.
I don’t think C++ is a bad language at all or anything. Just that it nudges developers into certain modes of thinking based on the features it chooses to focus on. Might explain why some developers still choose C.
What this comment looks like to a C programmer:
>> C89
> agree :D
> by welding a vtable onto every type
It's not true. Virtual methods table is present only for classes with at least one virtual method. Learn C++ properly before doing such claims.
The context you snipped is about dynamic dispatch, that is what you - as someone who has "learned C++ properly" apparently call "classes with at least one virtual method" and indeed the earlier comment calls "virtual interfaces".
The convenience of having regular generics and dyn generics handled automatically is a great feature of Rust, sure, however you can write a template in C++ that directly calls a method, f.e. obj.Object::method(), which skips the vtable, achieving the same thing. Or you can keep manually writing everything in C because you refuse to learn C++.
Your example is disingenuous. What you are stating is the obvious trivial way of doing something when your objective is actually quite different.
You can get exactly what you are asking for in C++ using techniques of static polymorphism and CRTP pattern (https://en.wikipedia.org/wiki/Curiously_recurring_template_p... and https://en.wikipedia.org/wiki/Barton%E2%80%93Nackman_trick) along with traits and dynamic dispatch (if needed).
For great examples of the above, see the classic Scientific and Engineering C++: An Introduction with Advanced Techniques and Examples by Barton & Nackman (1994).
What you're describing is needing to use specific verbose patterns to opt out of the defaults that do more complex things under the hood, whereas they're describing how C and Rust do not do those things by default and instead let you opt into them. It's not disingenuous to point that out.
Not quite.
What i am pointing out are neither complex nor any magic under-the-hood. They are simply techniques in the C++ repertoire well-known since the early 90's and used in all high-performance C++ libraries.
tialaramex made a big deal out of overheads in C++ dynamic dispatch (which incidentally are pretty minimal) using a trivial example, when performance focused (both time and size) C++ programmers do not use it that way at all. Modeling in C++ can be done in any number of ways and is driven by specific perspectives of the end goal.
Except that C++ provides the tools to do just like C, Rust, or whatever one feels like doing for dispatching, even if it requires a few pages of template metaprogramming mixed with compile time executions, or writing exactly the same C code on the common subset across both languages.
Now with reflection even more tools will be available.
Which is why despite all its warts and security flaws, many inherited from C source code compatibility, many domains will keep using it, because they will complain about their missing 1% that no one else uses.
Because these are General Purpose languages you can do the same things, but the contrast here is what's provided in the box and how it is used idiomatically, because in practice that's what gets used, and that's what I explained above.
You can write C++ style OOP hierarchy code in Rust but that's not idiomatic, and you can write Rust style explicit dynamic dispatch in C++ but again it isn't idiomatic.
Microsoft uses Rust like traits on Windows with C++.