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?