Is there any way to cast a std::any containing a derived pointer to a base pointer, without knowing the derived type?
This is unachievable. It is only possible to get an object out from std::any
using exactly the type that was put inside. Thus, you must know the type to get anything out of it.
It seems that std::any
does not fit your use case.
I'm late to the party, just just came across this question looking for an answer myself.
Here's what I eventually worked out.
I needed to pass either a wxMenuItem*, or a pointer to some sort of control (all are derived from wxControl) to my function in an std::any
. I know if I have a wxMenuItem* or a control based on flags. So ignore wxMenuItem* 's for now.
As my code only has to work with what a base class wxControl has, (and I imagine since your hammers share a common base class, you only want to do base hammer type stuff), I...
static_cast<wxControl*>(myCombo*)
at the calling point, and
auto myPtr {std::any_cast<wxControl*>(theAnyField)};
at the called point, and voila.