A bit of an obscure issue here but I need a way to cast std::any
to its base class, without knowing what derived class it is.
In other words, given a base class:
struct HCallable {
int numArgs;
std::vector<std::type_info> argTypes;
virtual std::any call(std::vector<std::any> args)=0;
};
And an instance of one of its (numerous) derived classes:
class in : public HCallable {
public:
int numArgs=1;
std::any call(std::vector<std::any> args) {
std::cout << huff::anyToString(args[0]);
std::string result;
std::getline(std::cin, result);
return result;
}
};
std::any instance = std::any(new in());
How can I take this instance, and convert it into an object of type HCallable
without knowing that instance
has type in
.
This code works if only I can find a way not to explicitly state in*
:
HCallable* func = dynamic_cast<HCallable*>(std::any_cast<in*>(instance));