Have you looked at boost::variant<>? You would need to make a few code changes:
typedef boost::variant<double, long, int, float> ParamValue;
After doing this, you no longer need a BaseParamValue or any inheritance at all for that matter.
class Param
{
public:
Param() : mParamValue(0) {}
Param(const string& type)
: mParamValue(create_param(type))
{
}
private:
ParamValue create_param(const string& type)
{
if (type == "LONG") return ParamValue(0L);
else if (type == "DOUBLE") return ParamValue(0.0);
//etc
}
ParamValue mParamValue;
};
class TestVisit : public boost::static_visitor<>
{
void operator()(double d) const
{
}
void operator()(long l) const
{
}
void operator(float f) const
{
}
};
class TestVisitReturn : public boost::static_visitor<bool>
{
bool operator()(double d) const
{
return true;
}
bool operator()(long l) const
{
return true;
}
bool operator(float f) const
{
return true;
}
};
Alternatively, you could templatize the operator() to accept any of the types.
int main(int argc, char** argv)
{
ParamValue value("LONG");
boost::apply_visitor(TestVisit(), value);
bool result = boost::apply_visitor(TestVisitReturn(), value);
}