I want to have a class Boxed_value that can InBox other types (starting from int, double, string...), similar to object in C#. This class type can be further passed to functions that Unbox it. These functions will receive a reference:
void receiver( Boxed_value & bv ) {}
So, it shouldn't be a template. Or? I need a recipe... The UnBox function can be separate, and templated. In this case receiver can get 2 parameters:
void receiver( Boxed_value & bv, Engine & eng ) {}
The implementation below is dummy and it's not compiling.
struct Boxed_value {
void inbox(int v) { _iv = v; }
void inbox(double v) { _dv = v; }
void inbox(std::string & v) { _sv = v; }
int _iv;
double _dv;
std::string _sv;
};
struct Engine {
template<typename T>
T unbox(Boxed_value bv) {
if (std::is_same<T, typeid(bv._iv).name()>::value) { return _iv };
if (std::is_same<T, typeid(bv._dv).name()>::value) { return _dv };
if (std::is_same<T, typeid(bv._sv).name()>::value) { return _sv };
}
};