// // Selects algorithm based on type // #include // default algorithm template struct select_algo{ template static void algo_compute(T& obj) { std::cout<<"Using default algo()\n"; // put here default implementation // or a function call, such as // default_algo(T& obj); } }; // use this if typename T supports optimized algorithm template<> struct select_algo { template static void algo_compute(T& obj) { obj.algo(); // assumes obj has algo as a member function } }; // default template struct use_optimized { static const bool opt = false; }; // generic "algo" template void algo(T& obj) { select_algo::opt>::algo_compute(obj); } class A { public: void algo() { std::cout<<"Using algo() optimized for A\n"; } }; class B { public: }; // specialization of use_optimized for A template<> struct use_optimized { static const bool opt = true; }; int main() { A a; B b; algo(a); // will use optimized algo algo(b); // will use default algo }