AMDG
I've been asked about a function for downcasting
an any several times, and I finally implemented it.
Short story: you can now use dynamic_any_cast to
convert between two arbitrary any types.
Here's a quick example which tries to cast a
boost::any equivalent to a boost::function
equivalent.
using plain_any = any,
relaxed> >;
template<class Sig>
using any_function = any,
relaxed> >;
bool try_call(const plain_any& arg) {
try {
auto fun = dynamic_any_cast >(arg);
fun();
} catch(bad_any_cast&) {
return false;
}
return true;
}
void global_register() {
register_binding, void(*)()>();
register_binding, void(*)()>();
register_binding, void(*)()>();
}
void do_something() {
std::cout << 42 << std::endl;
}
int main() {
global_register();
plain_any x = &do_something;
if(try_call(x)) {
std::cout << "x is a function" << std::endl;
}
plain_any y = 10;
if(!try_call(y)) {
std::cout << "y is NOT a function" << std::endl;
}
}
The calls to register_binding are necessary for
this to work, unfortunately. I don't think it's
possible to implement dynamic_any_cast without
some kind of registration, but at least it only
needs to be called once for each concept/type
combination. If anyone has any better ideas
about how to handle this, I'd love to hear it.
Comments, questions, and criticism are welcome.
In Christ,
Steven Watanabe