C++: Template duck-typing in 30 lines

#include struct Duck { void quack(std::ostream& out) { out << “Quack!” << std::endl; } }; struct Platypus { void quack(std::ostream& out) { int* i = NULL; out << *i << std::endl; } }; template void do_quack(std::ostream& out, MaybeDuck duck) { duck.quack(out); } int main() { Duck duck; Platypus platypus; do_quack(std::cout, duck); do_quack(std::cout, platypus); } C++ […]