In C++, I am trying to implement my own any
class using C++. However, before I was able to test it (so if my implementation is bad, feel free to correct me), I got the error: error C2228: left of '.val' must have class/struct/union
twice from using the value()
function twice, which seems weird when it works everywhere else. The only thing I could think of would be that decltype
infront of a function is causing an error, but it shouldn't:
Edit: I have updated the way of changing the variable for the template<class T> any(T V){...}
constructor
class any{
protected:
template<class T> struct variable{
public:
T val;
variable(){}
variable(T t) : val(t){}
};
variable<int> v;
public:
any(){
v.val = 0;
}
template<class T> any(T V){
variable<T> nV(V);
v = nV;
}
~any(){
delete &v;
}
decltype(v.val) value(){ // Error still here
return v.val;
}
template<class T> static any create(T V){
return any(V);
}
};