Facebook
From Lousy Macaw, 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 121
  1. #include <iostream>
  2.  
  3. template<class T>
  4. void hello(T val) {
  5.         std::cout << "Hello: " << val << "\n";
  6. }
  7.  
  8. template<class T>
  9. void bye(T val) {
  10.         std::cout << "Bye: " << val << "\n";
  11. }
  12.  
  13. template<class T>
  14. struct HelloSayer {
  15.         void operator()(T val) {
  16.                 hello(val);
  17.         }
  18. };
  19.  
  20. template<class T>
  21. struct ByeSayer {
  22.         void operator()(T val) {
  23.                 bye(val);
  24.         }
  25. };
  26.  
  27. template<template<class> class Function>
  28. void sayHelloOrBye(int val) {
  29.         Function<int>()(val);
  30. }
  31.  
  32. int main() {
  33.         sayHelloOrBye<HelloSayer>(4);
  34.         sayHelloOrBye<ByeSayer>(5);
  35. }