fork download
  1. #include <functional>
  2. #include <iostream>
  3.  
  4. void sampleFunction()
  5. {
  6. std::cout << "This is the sample function!\n";
  7. }
  8.  
  9. void checkFunc(std::function<void()> const& func)
  10. {
  11. // Use operator bool to determine if callable target is available.
  12. if (func)
  13. {
  14. std::cout << "Function is not empty! Calling function.\n";
  15. func();
  16. }
  17. else
  18. std::cout << "Function is empty. Nothing to do.\n";
  19. }
  20.  
  21. int main()
  22. {
  23. std::function<void()> f1 = []{};
  24. std::function<void()> f2(sampleFunction);
  25.  
  26. std::cout << "f1: ";
  27. checkFunc(f1);
  28.  
  29. std::cout << "f2: ";
  30. checkFunc(f2);
  31. }
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
f1: Function is not empty! Calling function.
f2: Function is not empty! Calling function.
This is the sample function!