-
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
Variant upcasting #81
Comments
Mhm...I think you have two options:
Does this help? |
Thanks for the quick reply! Option 2 is almost out of the question since the serializer should ideally not have to know about the types it will handle beforehand. Option 1 sounds good but the problem I'm seeing is how to retain ownership of the wrapped data. Based on local testing, it seems that the proposed series of conversions would yield a |
Why should the variant be destructed, when you still hold the original one, everything should be fine. |
My use case is serialization and deserialization of a
Regarding the above question of ownership, I can usually do casts like this:
The same doesn't work when I work with variants and cast from |
Okay, what I can read now from your last answer is, that you want to perform a downcast, not an upcast. Then take a look at my serialization code at: Does this help you? |
The downcast during serialization is not an issue since, as you pointed out, rttr provides a method to determine the most derived type. However, during deserialization I'll start with the correct type ( The use-case is similar to the JSON serialization example that you linked to, but let's look at from_json.cpp#L179 instead. This wouldn't work if the serialized object was of type As another example, consider serializing a class with a
Again, the issue is not serialization but deserialization. I can create |
Sure it would work.
Regarding the 2nd issue, this is indeed a problem. I have to think about it. |
Yes, serialization isn't a problem. However, I won't be able to call |
got it now....still have to think about a nice solution for it. |
IMHO this would work well if it was possible to do |
@acki-m, any new plans on this? |
I have something coded, it will basically add the conversion function, which you can already create by yourself, automatically. Is it urgent, do you use RTTR for some private project or work related? |
Cool! It's not urgent (it's not blocking my current work) but would be nice to have eventually :) Would happy to use a workaround as well (e.g. a standalone conversion function). |
Just wanted to check whether you've got plans to proceed on this one soon. Otherwise, I'm also happy to add a conversion function if that solves the upcasting issue. |
It's done, and merged into master, I hope it's what you had in mind. |
Hi there! I'm trying to cast a variant hold a
shared_ptr<Derived>
of a derived class to ashared_ptr<Base>
of its base class (i.e. upcasting). I've searched previous issues and it seems like others attempted this as well, e.g. in #56. Similar to #56, my use-case concerns object serialization: I would like to serialize and deserialize an object ofDerived
via ashared_ptr<Base>
. Serialization is fine as I can easily obtain the most derived type of a given variant, but deserialization (obtainingshared_ptr<Base>
from a reconstructedshared_ptr<Derived>
RTTR variant) does not seem to work.The documentation of
variant::convert
mentions that it supports "Conversion of raw pointers to its derived types, if a rttr_cast to the type described by target_type would succeed," but I'm wondering why there's no support for casting to base types.The text was updated successfully, but these errors were encountered: