fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #if 0
  5. template <class F1, class F2>
  6. struct overload : F1, F2 {
  7. overload(F1 f1, F2 f2) : F1(f1), F2(f2) { }
  8.  
  9. using F1::operator();
  10. using F2::operator();
  11. };
  12.  
  13. template <class F1, class F2>
  14. auto make_overload(F1 f1, F2 f2) {
  15. return overload<F1, F2>(f1, f2);
  16. }
  17. #else
  18. template <class... Fs>
  19. struct overload;
  20.  
  21. template <class F0, class... Frest>
  22. struct overload<F0, Frest...> : F0, overload<Frest...> {
  23. overload(F0 f0, Frest... rest) : F0(f0), overload<Frest...>(rest...) {}
  24.  
  25. using F0::operator();
  26. using overload<Frest...>::operator();
  27. };
  28.  
  29. template <class F0>
  30. struct overload<F0> {
  31. overload(F0 f0) : F0(f0) {}
  32. using F0::operator();
  33. };
  34.  
  35. template <class... Fs>
  36. auto make_overload(Fs... fs) {
  37. return overload<Fs...>(fs...);
  38. }
  39. #endif
  40.  
  41. #if 0
  42. #define CAP
  43. #define PRINTY()
  44. #else
  45. #define CAP =
  46. #define PRINTY() cout << "int y==" << y << endl
  47. #endif
  48.  
  49. int main(int argc, char *argv[]) {
  50. int y = 123;
  51.  
  52. auto f = make_overload(
  53. [CAP] (int x) { cout << "int x==" << x << endl; PRINTY(); },
  54. [CAP] (char *cp) { cout << "char *cp==" << cp << endl; PRINTY(); });
  55. f(argc);
  56. f(argv[0]);
  57. }
Compilation error #stdin compilation error #stdout 0s 3460KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of 'struct overload<main(int, char**)::<lambda(char*)> >':
prog.cpp:22:8:   required from 'struct overload<main(int, char**)::<lambda(int)>, main(int, char**)::<lambda(char*)> >'
prog.cpp:37:31:   required from 'auto make_overload(Fs ...) [with Fs = {main(int, char**)::<lambda(int)>, main(int, char**)::<lambda(char*)>}]'
prog.cpp:54:69:   required from here
prog.cpp:32:22: error: type 'main(int, char**)::<lambda(char*)>' is not a base type for type 'overload<main(int, char**)::<lambda(char*)> >'
   using F0::operator();
                      ^
prog.cpp: In instantiation of 'struct overload<main(int, char**)::<lambda(int)>, main(int, char**)::<lambda(char*)> >':
prog.cpp:37:31:   required from 'auto make_overload(Fs ...) [with Fs = {main(int, char**)::<lambda(int)>, main(int, char**)::<lambda(char*)>}]'
prog.cpp:54:69:   required from here
prog.cpp:26:38: error: no members matching 'overload<main(int, char**)::<lambda(char*)> >::operator()' in 'struct overload<main(int, char**)::<lambda(char*)> >'
   using overload<Frest...>::operator();
                                      ^
prog.cpp: In function 'int main(int, char**)':
prog.cpp:56:11: error: no match for call to '(overload<main(int, char**)::<lambda(int)>, main(int, char**)::<lambda(char*)> >) (char*&)'
  f(argv[0]);
           ^
prog.cpp:53:15: note: candidate: main(int, char**)::<lambda(int)> <near match>
   [CAP] (int x) { cout << "int x==" << x << endl; PRINTY(); },
               ^
prog.cpp:53:15: note:   conversion of argument 1 would be ill-formed:
prog.cpp:56:10: error: invalid conversion from 'char*' to 'int' [-fpermissive]
  f(argv[0]);
          ^
prog.cpp: At global scope:
prog.cpp:23:3: error: 'overload<F0, Frest ...>::overload(F0, Frest ...) [with F0 = main(int, char**)::<lambda(int)>; Frest = {main(int, char**)::<lambda(char*)>}]', declared using local type 'main(int, char**)::<lambda(int)>', is used but never defined [-fpermissive]
   overload(F0 f0, Frest... rest) : F0(f0), overload<Frest...>(rest...) {}
   ^
stdout
Standard output is empty