polymorphic_value
polymorphic_value copied to clipboard
Static and dynamic casts
The infrastructure that supports the polymorphic_value<T>::polymorphic_value(polymorphic_value<U>&&)
constructor template (which is surprisingly heavy, given that repeated invocations build a linked list of type-conversion objects) could also be used to provide function templates
template<class T, class U>
polymorphic_value<T> static_polymorphic_cast(polymorphic_value<U>);
template<class T, class U>
polymorphic_value<T> dynamic_polymorphic_cast(const polymorphic_value<U>&);
template<class T, class U>
polymorphic_value<T> dynamic_polymorphic_cast(polymorphic_value<U>&&);
A similar set appeared in R0 of the paper, but the semantics here might be a bit different, mostly because of the possibility of rvalue arguments:
-
static_polymorphic_cast
would allow apolymorphic_value<Widget>
known to hold (say) aDialog
orTooltip
to be converted into apolymorphic_value<Window>
that would persistently support whatever additional operations associated withWindow
. (It would avoid creating any new objects, unless a small-buffer optimization intervened!) -
dynamic_polymorphic_cast
would let a set ofpolymorphic_value<Widget>
be filtered intopolymorphic_value<Window>
andpolymorphic_value</* other */>
sets.
These make sense even with the value-not-pointer interpretation of the class template as a whole.
Do you have use cases that you can share?
Any extra experience/examples would be really useful.
Not really; I just had to implement something very much like polymorphic_value
myself that supported serialization (I used the cute old name idea of some
), and thinking about the complexity/overhead necessary to support the "generalizing" constructor made me realize that it could support more than that (in particular, the inverse of that process).
Musing on this, these cast operators are impossible for users to add as they require access to internals of polymorphic_value
. If there is a good motivating use case, then they should be added.
Closing this issue in the absence of a motivating use case.