-
Notifications
You must be signed in to change notification settings - Fork 449
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Polymorphic assignment of raw pointer properties does not work #56
Comments
Hi Rovarma, That's actually not a bug it is desired behavior. However, what you want to do Is possible with an additional step and even more dynamically then you might think:
What do you think, can you live with this? PS: When creating a variant, with a pointer type (i.e. type <=8 bytes), it will don't create any heap allocation, so it it quite fast. |
Hey, Thanks, I didn't know about convert. That might be fine for us; I'll test it later. Yes, I do know about I'll keep you posted. |
I guess this is more a bug + feature request rolled into one.
First, the RTTR equivalent of the following regular C++ code does not work (don't have a working sample right now, but I think you get it):
So, we're new'ing a Derived, then assigning that to a pointer-to-base through RTTR.
In addition, the following situation does not work in either C++ or RTTR, but can work in RTTR and is extremely useful for things like object deserialization, so consider this a feature request:
So, we're new'ing a Derived, then assigning that to a pointer-to-base, then trying to assign that to a pointer-to-derived. In C++ this is illegal, because the compiler can't know that
Base*
is actually a validDerived*
instance (so it requires an explicit cast), but in RTTR we can know that this is a valid assignment, since we know the actual type of the instance.The first bug (not being able to assign pointer-to-derived to pointer-to-base) is caused by the fact that
ptr_type<T> argument::is_type()
returns false when faced with this situation. That, in turn, is caused by the implementation to be wrong:Specifically it's about the
rttr::type::get<T>() == m_type
line. This is a specific type match, which in this situation will return false since the type does not actually match exactly. Instead, the code should check if the types match through ais_derived_from
check, ie:This can be further extended to also implement the 'feature request': by not checking m_type directly, but instead using the most-derived-type of the pointer being assigned, we can also assign pointer-to-base to pointer-to-derived:
Now, while I've tested this code and it works for us, I'm less sure about it because I don't know how it behaves in the presence of multiple base classes. In theory it should work fine, but I don't know enough about multiple base classes to say for sure (I barely ever use them).
The text was updated successfully, but these errors were encountered: