Using boost (maybe mpl?) to generalize a switch statement to call different template functions

Hi, I have the following program, which uses a switch statement to call different template functions. The example is simple, but as the number of shapes increase, the switch statement might be very hard to maintain. I'm looking for a more general solution (maybe using mpl, not sure), such that the switch statement can be automatically generated, therefore, more maintainable. Thanks, Peng #include <iostream> enum shape { circle, square // there might be many cases }; template <shape, shape> struct A { void doit() const; }; //doit functions are defined for a majority number of shape combinations (maybe not all, but could be defined for all---the bodies of the functions not need can be empty). // 3 case. There should be 4 doit's if we consider all combinations. template <> void A<circle, circle>::doit() const { std::cout << "circle circle" << std::endl; } template <> void A<circle, square>::doit() const { std::cout << "circle square" << std::endl; } template <> void A<square, circle>::doit() const { std::cout << "square circle" << std::endl; } void f(shape s1, shape s2) { // each defined shape-combination corresponds to one case // I want to generalize the code by using boost::mpl // (or some other packages if applicable) switch(s1 << 8 | s2) { case circle << 8 | circle: A<circle, circle>().doit(); break; case circle << 8 | square: A<circle, square>().doit(); break; case square << 8 | circle: A<square, circle>().doit(); break; default: std::cout << "not defined" << std::endl; } } int main() { f(circle, circle); f(circle, square); f(square, circle); f(square, square); }

Peng Yu wrote:
I have the following program, which uses a switch statement to call different template functions. The example is simple, but as the number of shapes increase, the switch statement might be very hard to maintain.
I'm looking for a more general solution (maybe using mpl, not sure), such that the switch statement can be automatically generated, therefore, more maintainable.
void f(shape s1, shape s2) { // each defined shape-combination corresponds to one case // I want to generalize the code by using boost::mpl // (or some other packages if applicable) switch(s1 << 8 | s2) { case circle << 8 | circle: A<circle, circle>().doit(); break; case circle << 8 | square: A<circle, square>().doit(); break; case square << 8 | circle: A<square, circle>().doit(); break; default: std::cout << "not defined" << std::endl; } }
int main() { f(circle, circle); f(circle, square); f(square, circle); f(square, square); }
I believe this is essentially a double-dispatch problem, though you're using enum values rather than distinct subclass types. Scott Meyers talks about double dispatch (in More Effective C++? I don't have it with me). At that point he was advocating a map of target functions, keyed by type pairs. Does MPL give us a more robust way to address multiple dispatch?

Nat Goodspeed wrote:
Peng Yu wrote:
I have the following program, which uses a switch statement to call different template functions. The example is simple, but as the number of shapes increase, the switch statement might be very hard to maintain.
I'm looking for a more general solution (maybe using mpl, not sure), such that the switch statement can be automatically generated, therefore, more maintainable.
void f(shape s1, shape s2) { // each defined shape-combination corresponds to one case // I want to generalize the code by using boost::mpl // (or some other packages if applicable) switch(s1 << 8 | s2) { case circle << 8 | circle: A<circle, circle>().doit(); break; case circle << 8 | square: A<circle, square>().doit(); break; case square << 8 | circle: A<square, circle>().doit(); break; default: std::cout << "not defined" << std::endl; } }
int main() { f(circle, circle); f(circle, square); f(square, circle); f(square, square); }
I believe this is essentially a double-dispatch problem, though you're using enum values rather than distinct subclass types. Scott Meyers talks about double dispatch (in More Effective C++? I don't have it with me). At that point he was advocating a map of target functions, keyed by type pairs.
Does MPL give us a more robust way to address multiple dispatch?
I think that was "Modern C++ Design" by Alexandrescu. Jeff Flinn

Jeff Flinn wrote:
Nat Goodspeed wrote:
I believe this is essentially a double-dispatch problem, though you're using enum values rather than distinct subclass types. Scott Meyers talks about double dispatch (in More Effective C++? I don't have it with me). At that point he was advocating a map of target functions, keyed by type pairs.
I think that was "Modern C++ Design" by Alexandrescu.
He talks about it too. :-)
participants (3)
-
Jeff Flinn
-
Nat Goodspeed
-
Peng Yu