2016-06-27 15 views
5

mam doładowania definicji typu Wariant:Jak uprościć akcję dodatnią w wariancie doładowania?

typedef boost::variant< bool, int, float, double> VariantType; 

Chcę zaimplementować Dodaj/odejmij/mnożenie/akcję podzielić na nim. Weź Dodaj klasę na przykład. Problem polega na tym, że dodając nowy typ do VariantType, na przykład std :: string, klasa Add musi zostać zaktualizowana o nowy typ.

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"); 
    } 
}; 

Wykorzystanie:

VariantType v1 = 1; 
VariantType v2 = 2.1; 
VariantType v3 = boost::apply_visitor(Add(), v1, v2); 
cout<<boost::get<double>(v3)<<endl; //Print 2.2 

wersja GCC 4.8.2 jest; Wersja doładowania to 1.57.0; Jak po prostu dodać zajęcia? Dzięki.

Odpowiedz

3

Po prostu zrób to polimorficzny funktor:

Live On Coliru

#include <boost/variant.hpp> 
#include <iostream> 

using VariantType = boost::variant<int, float, double, bool>; 

struct Add : public boost::static_visitor<VariantType> { 
    template <typename T, typename U> 
    auto operator() (T a, U b) const -> decltype(a+b) { return a + b; } 

    template <typename T> VariantType operator()(bool, T) const { throw std::invalid_argument("Can't to bool"); } 
    template <typename T> VariantType operator()(T, bool) const { throw std::invalid_argument("Can't add bool"); } 
    VariantType      operator()(bool,bool) const { throw std::invalid_argument("Can't add bools"); } 
}; 

int main() { 
    std::cout << std::boolalpha; 

    VariantType specimens[] = { int(42), 3.14f, 3.14, true }; 
    for (auto lhs : specimens) 
     for (auto rhs : specimens) 
     { 
      try { 
       std::cout << lhs << " + " << rhs << " == " << boost::apply_visitor(Add{}, lhs, rhs) << "\n"; 
      } catch(std::exception const& e) { 
       std::cout << lhs << " + " << rhs << " ==> " << e.what() << "\n"; 
      } 
     } 
} 

Drukuje:

42 + 42 == 84 
42 + 3.14 == 45.14 
42 + 3.14 == 45.14 
42 + true ==> Can't add bool 
3.14 + 42 == 45.14 
3.14 + 3.14 == 6.28 
3.14 + 3.14 == 6.28 
3.14 + true ==> Can't add bool 
3.14 + 42 == 45.14 
3.14 + 3.14 == 6.28 
3.14 + 3.14 == 6.28 
3.14 + true ==> Can't add bool 
true + 42 ==> Can't to bool 
true + 3.14 ==> Can't to bool 
true + 3.14 ==> Can't to bool 
true + true ==> Can't add bools 
+0

Właśnie dodałem na żywo przykład z wielu drobnych poprawek/wyjaśnień/cm @ DanielSchepler – sehe

+0

To działa. Dzięki – liuzw