On Mon, Apr 15, 2024 at 4:48 PM Jason McKesson via Std-Proposals <std-proposals@lists.isocpp.org> wrote:
For places where what the OP is trying to do makes any kind of sense,
I feel like storing a polymorphic<B> inside the any is the right way
to go. The receiving code knows to expect a polymorphic value of a
particular base class, and the sending code is obligated to use that
kind of object.

Consider a class hierarchy of five classes:

class A {};
class B : public A {};
class C: public B {};
class D : public A {}
class E : public B {}

and three functions:

void foo(A&); // Knows how to handle all As
void bar(B&); // Knows how handle all Bs, but doesn't know how to handle Ds
void baz(C&); // Knows how to handle all Cs, but doesn't know how to handle Es

I hope we can all agree that it would be bad practice for the authors of bar() and baz() to change them to take A& and then dynamic_cast the input to B& and C& respectively.

Then why would it be good practice, if we transform these functions to take in an any* or any&, to force them to perform a widening any_cast to polymorphic_value<A> followed by a dynamic_cast<B&> or dynamic_cast<C&> respectively?


This electronic communication and the information and any files transmitted with it, or attached to it, are confidential and are intended solely for the use of the individual or entity to whom it is addressed and may contain information that is confidential, legally privileged, protected by privacy laws, or otherwise restricted from disclosure to anyone else. If you are not the intended recipient or the person responsible for delivering the e-mail to the intended recipient, you are hereby notified that any use, copying, distributing, dissemination, forwarding, printing, or copying of this e-mail is strictly prohibited. If you received this e-mail in error, please return the e-mail to the sender, delete it from your computer, and destroy any printed copy of it.