
nowake@fiercewinds.net wrote:
I create multi method for boost::any using Modern C++ Design's multi method technique.
It can dispatch method dynamically like this: _________________________________________________________________________ struct T1 { string operator()(int value) const { return string("T1(int)"); }; string operator()(double value) const { return string("T1(double)"); }; string operator()(A value) const { return string("T1(A)"); }; }; // "entry" to Multi method bool t10(Multimethod::entry<any, any, T1, int>()); bool t11(Multimethod::entry<any, any, T1, double>()); bool t12(Multimethod::entry<any, any, T1, A>());
struct T2 { string operator()(int value) const { return string("T2(int)"); }; }; // "entry" to Multi method bool t20(Multimethod::entry<any, any, T2, int>());
BOOST_AUTO_TEST_CASE(test04) { any m((T1())); any a(0); BOOST_CHECK_EQUAL( any_cast<string>(Multimethod::apply<any>(m, a)), "T1(int)"); a = 0.0; BOOST_CHECK_EQUAL( any_cast<string>(Multimethod::apply<any>(m, a)), "T1(double)"); a = A(); BOOST_CHECK_EQUAL( any_cast<string>(Multimethod::apply<any>(m, a)), "T1(A)");
m = T2(); a = 0; BOOST_CHECK_EQUAL( any_cast<string>(Multimethod::apply<any>(m, a)), "T2(int)"); };
This looks overcomplicated. A while ago, I offered something like this any m = 0; apply_visitor<int, double, A>(f, m); or alternatively, apply_visitor(f, m); with f specifying all overloads in some compile-time reflection structure. unfortunately, it cannot be made constant-time because std::type_info sucks. Is your solution constant-time?