3 ms·
Can you explain?
by jeffdavis 7y ago
Can you explain?
- saagarjha 7y agoRust 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 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.
- steveklabnik 7y agoIt 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.
- jeffdavis 7y ago"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.