polymorphic_value icon indicating copy to clipboard operation
polymorphic_value copied to clipboard

Static and dynamic casts

Open opensdh opened this issue 3 years ago • 3 comments

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:

  1. static_polymorphic_cast would allow a polymorphic_value<Widget> known to hold (say) a Dialog or Tooltip to be converted into a polymorphic_value<Window> that would persistently support whatever additional operations associated with Window. (It would avoid creating any new objects, unless a small-buffer optimization intervened!)
  2. dynamic_polymorphic_cast would let a set of polymorphic_value<Widget> be filtered into polymorphic_value<Window> and polymorphic_value</* other */> sets.

These make sense even with the value-not-pointer interpretation of the class template as a whole.

opensdh avatar Jan 22 '22 00:01 opensdh

Do you have use cases that you can share?

Any extra experience/examples would be really useful.

jbcoe avatar Jan 22 '22 11:01 jbcoe

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).

opensdh avatar Jan 25 '22 19:01 opensdh

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.

jbcoe avatar Jul 06 '22 10:07 jbcoe

Closing this issue in the absence of a motivating use case.

jbcoe avatar Aug 28 '22 16:08 jbcoe