fork download
  1. #include <iostream>
  2. #include <utility>
  3.  
  4. struct S { int i; bool b; char c; };
  5.  
  6. // Type-dependent processors.
  7. void func(const int&) { std::cout << "func(const int&)\n"; }
  8. void func(const bool&) { std::cout << "func(const bool&)\n"; }
  9. void func(const char&) { std::cout << "func(const char&)\n"; }
  10. void func(const double&) { std::cout << "func(const double&)\n"; }
  11. void func(const S&) { std::cout << "func(const S&)\n"; }
  12.  
  13. // Main function, defers to processor.
  14. template<typename T> void(*var)(const T&) = func;
  15. template<typename T> void call(T&& t) { return var<T>(std::forward<T>(t)); }
  16.  
  17. // Put it to use.
  18. int main() {
  19. call(3);
  20. call(S{});
  21. call(true);
  22. call(3.);
  23. call('3');
  24. }
Success #stdin #stdout 0.01s 5328KB
stdin
Standard input is empty
stdout
func(const    int&)
func(const      S&)
func(const   bool&)
func(const double&)
func(const   char&)