Hi,
I have two classes, an pure abstract class CPPVar and another class CPPVarT defined like this:
1 2 3 4 5 6 7 8
template <typename T>
class CPPVarT : public CPPVar
{
public:
/* Methods and constructors */
private:
std::shared_ptr<T> data_;
};
I would like to implement a templated method on type V in CPPVar or function which return the data in CPPVarT typecasted to V* if possible, or NULL if not. The signature may be like this if implemented as a method:
1 2
template <typename V>
V* CPPVar::cast();
I know how to do if V == T but I would like the method to work even if V is a base type of T. I thought of virtual methods but templeted virtual methods are forbiden. Is there a way to do this(and it must be with any type, without using a type traits structure)?