Re:“C is how the computer works” is a dangerous mindset for ...
by saagarjha on Tuesday March 31, 2020 @04:04PM
(#22741878)
Rust has both static (based on monomorphization, similar to a template instantiation in C++) and dynamic (based on vtables, similar to inheritance in C++) dispatch: https://doc.rust-lang.org/1.8.0/book/trait-objects.html. Both Rust and C++ are moving in the direction of encouraging static dispatch.
[ Parent
| on HN ]
Re:“C is how the computer works” is a dangerous mindset for ...
by steveklabnik on Tuesday March 31, 2020 @04:48PM
(#22742393)
It may be similar, but it is implemented very differently, in exactly the "so can always work on unmodified structures" sense.
Trait objects are a double pointer: one to the vtable, and one to the data. C++'s dynamic dispatch uses a single pointer to a structure that has the vtable and the data. The memory layouts are very different.
Re:“C is how the computer works” is a dangerous mindset for ...
by jeffdavis on Tuesday March 31, 2020 @05:53PM
(#22743077)
"The memory layouts are very different."
Clarification: the memory layouts between C++ dynamic dispatch and rust dynamic dispatch are very different.
Rust imposes no requirements on the structure layout, whereas C++ adds some magic data. The magic data means it's awkward to make C++ do dynamic dispatch on a C struct pointer; but rust can do dynamic dispatch on a C struct pointer with no problem.