fork download
  1. #include <iostream>
  2. #include <array>
  3. #include <cassert>
  4.  
  5. using namespace std;
  6.  
  7. template<typename Q, void (*foo_p)(Q)>
  8. void wrapped(int x){ foo_p(x); }
  9.  
  10. void foo1(int x){
  11. if(x < 0){
  12. cout << "foo(negative numnber)==woot!?\n";
  13. } else {
  14. for(size_t i=0; i < x; i++)
  15. cout << i << ". foo1(" << x << ")\n";
  16. }
  17. }
  18. void foo2(double x){
  19. cout << "foo2(" << x << ")\n";
  20. }
  21.  
  22. int main(){
  23. using foo_t = void (*)(int);
  24. std::array<foo_t, 2> arr;
  25.  
  26. arr[0] = wrapped<int, foo1>;
  27. arr[1] = wrapped<double, foo2>;
  28.  
  29. size_t choice_idx;
  30. int choice_input;
  31. cout << "choose an idx: 0, or 1>>";
  32. cin >> choice_idx;
  33. cout << "\nand an input int>>";
  34. cin >> choice_input;
  35. assert(choice_idx < arr.size());
  36.  
  37. cout << "\n\n";
  38. arr[choice_idx](choice_input);
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 3464KB
stdin
0
-5
stdout
choose an idx: 0, or 1>>
and an input int>>

foo(negative numnber)==woot!?