2 回答
Is this what you want?
struct Base
{
virtual void* convert(const std::type_info&) = 0;
};
template<typename T>
struct Derived : Base
{
virtual void* convert(const std::type_info& ti)
{ return typeid(T) == ti ? &t : nullptr; }
T t;
};
struct Any
{
Base* b;
template<typename U>
operator U()
{
if (auto p = b->convert(typeid(U)))
return *static_cast<U*>(p);
throw std::exception();
}
};
As the other answer says, it's hard to know exactly what you want as you've only shown invalid code, not explained what you're trying to achieve.
Edit oh I see now you want it to work for any convertible type, not just exact matches ... then no, you can't turn a type_info
back into the type it represents, which would be needed for the derived type to test if the given type_info
corresponded to a type that its stored type is convertible to. You need to know the correct type and specify it somehow, either explicitly or implicitly via deduction. If you then want to convert it to another type, you can do that separately:
Any a{ new Derived<int>() };
try {
char c = a; // throws
}
catch (...)
{
}
int i = a; // OK
char c = (int)a; // OK
Based on your update I think that this is what you are trying to do.
#include <typeinfo>
#include <exception>
#include <string>
template <typename T>
struct Any{
T* t;
Any():t(NULL){}
Any(const T& _t){
t=new T(_t);
}
template<typename U>
operator U(){
if(typeid(T)!=typeid(U) || t==NULL)
throw std::exception();
return *t;
}
};
int main (){
Any<std::string> a(std::string("Nothing"));
std::string b=a;
return 0;
};
If this doesn't help please explain in text not code what you are trying to achieve. It'll be useful to tell us also why you want to use those two extra classes Base and Derived.