У меня есть определение типа варианта повышения:
typedef boost::variant< bool, int, float, double> VariantType;
Я хочу реализовать на нем действие добавления/вычитания/умножения/разделения. Возьмем, к примеру, Добавить класс. Проблема заключается в том, что при добавлении нового типа в VariantType, например std::string, класс Add должен быть обновлен новым типом.
struct Add : public boost::static_visitor<VariantType> {
template <typename T>
T operator() (T a, T b) const {
return a + b;
}
float operator() (int a, float b) const {
return a + b;
}
float operator() (float a, int b) const {
return a + b;
}
double operator() (int a, double b) const {
return a + b;
}
double operator() (double a, int b) const {
return a + b;
}
double operator() (float a, double b) const {
return a + b;
}
double operator() (double a, float b) const {
return a + b;
}
VariantType operator() (bool a, int b) const {
throw std::invalid_argument("bool and int can't Plus");
}
VariantType operator() (bool a, float b) const {
throw std::invalid_argument("bool and float can't Plus");
}
VariantType operator() (bool a, double b) const {
throw std::invalid_argument("bool and double can't Plus");
}
VariantType operator() (int a, bool b) const {
throw std::invalid_argument("int and bool can't Plus");
}
VariantType operator() (float a, bool b) const {
throw std::invalid_argument("float and bool can't Plus");
}
VariantType operator() (double a, bool b) const {
throw std::invalid_argument("double and bool can't Plus");
}
};
Применение:
VariantType v1 = 1;
VariantType v2 = 2.1;
VariantType v3 = boost::apply_visitor(Add(), v1, v2);
cout<<boost::get<double>(v3)<<endl; //Print 2.2
Версия GCC — 4.8.2; Boost-версия — 1.57.0; Как просто добавить класс? Спасибо.