With the new AsType
function it’s possible to serialize an object as another (compatible) type, and is intended for invoking the serializer of a parent class.
Writing AsType<Parent>(child)
will work in any context:
READWRITE(AsType<Parent>(child))
s << AsType<Parent>(child)
s >> AsType<Parent>(child)
In case child is const
, the result will be a reference to a const Parent
type, resulting in const-correct behavior.
For now this primitive isn’t very useful, as the constness is statically known in each of the instances (so a simple cast would work). However, in #10785 I plan to modify the serialization code to have a single implementation which works on a const object when serializing and non-const when deserializing. AsType
behaves correctly in this case, maintaining the constness of the argument. It’s also safer, as this only involves automatic type conversions, and no explicit casts.