Well, you can't do that. The any mechanism works as follows:
struct base { virtual ~base() { } }; template<typename T> struct concrete_base : base { T t; concrete_base(T t):t(t) { } }; struct my_any { base * b; template<typename T> my_any(T t):b(new concrete_base<T>(t)) { } template<typename T> T any_cast() { concrete_base<T> * t = dynamic_cast< concrete_base<T>* >(b); if(!t) throw bad_any_cast(); return t->t; } };
I hope it is clear what this does. It is impossible to do what you are looking for, I think. The reason is that there is no type information that may be useful here. RTTI does not provide it.
source share