fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. class EG
  5. {
  6. template<class T>
  7. std::function<void(T)> f;
  8.  
  9. template<class T>
  10. void func(std::function<void(T)>&& arg)
  11. {
  12. if(f != nullptr)
  13. {
  14. f();
  15. std::cout << "f != nullptr" << std::endl;
  16. }
  17. f.swap(arg);
  18. }
  19.  
  20. };
  21.  
  22. int main(void)
  23. {
  24. EG eg;
  25. eg.func([](std::string s){});
  26. eg.func([](int i){});
  27.  
  28. return 0;
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:7:28: error: data member ‘f’ cannot be a member template
prog.cpp: In member function ‘void EG::func(std::function<void(T)>&&)’:
prog.cpp:12:10: error: ‘f’ was not declared in this scope
prog.cpp:17:7: error: ‘f’ was not declared in this scope
prog.cpp: In function ‘int main()’:
prog.cpp:25:28: error: no matching function for call to ‘EG::func(main()::<lambda(std::string)>)’
prog.cpp:25:28: note: candidate is:
prog.cpp:10:9: note: template<class T> void EG::func(std::function<void(T)>&&)
prog.cpp:10:9: note:   template argument deduction/substitution failed:
prog.cpp:25:28: note:   ‘main()::<lambda(std::string)>’ is not derived from ‘std::function<void(T)>’
prog.cpp:26:20: error: no matching function for call to ‘EG::func(main()::<lambda(int)>)’
prog.cpp:26:20: note: candidate is:
prog.cpp:10:9: note: template<class T> void EG::func(std::function<void(T)>&&)
prog.cpp:10:9: note:   template argument deduction/substitution failed:
prog.cpp:26:20: note:   ‘main()::<lambda(int)>’ is not derived from ‘std::function<void(T)>’
prog.cpp:28:9: error: expected ‘}’ at end of input
stdout
Standard output is empty