3 ms·
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 point
by steveklabnik 7y ago
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.
- 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.