fork download
  1. // functional_test.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <functional>
  5. #include <iostream>
  6.  
  7. template<class T1, class T2>
  8. inline void dump(const T1 &f1, const T2 &f2)
  9. {
  10. std::cout << std::boolalpha
  11. << "f1 : " << static_cast<bool>(f1) << std::endl
  12. << "f2 : " << static_cast<bool>(f2) << std::endl;
  13. }
  14.  
  15.  
  16. int main(int , char* [])
  17. {
  18. std::function<void(void*)> f1;
  19. std::function<void(const void*)> f2;
  20.  
  21. std::cout << "Initial state: " << std::endl;
  22. dump(f1,f2);
  23.  
  24. // Expected compilation fail
  25. //std::cout << "assign value of f1 to f2: " << std::endl;
  26. //f2 = f1;
  27. //dump(f1,f2);
  28.  
  29. std::cout << "Assign value of f2 to f1: " << std::endl;
  30. f1 = f2; // Could fail, but didn't
  31. dump(f1,f2); // Should give false/false but outputs true/false
  32.  
  33. try {
  34. if(f1)
  35. f1(nullptr);
  36. else
  37. std::cout << "Skip f1" << std::endl;
  38.  
  39. if(f2)
  40. f2(nullptr);
  41. else
  42. std::cout << "Skip f2" << std::endl;
  43. }
  44. catch(const std::exception &e) {
  45. std::cerr << "Exception: " << e.what() << std::endl;
  46. }
  47.  
  48. return 0;
  49. }
  50.  
  51.  
Success #stdin #stdout 0s 3144KB
stdin
Standard input is empty
stdout
Initial state: 
f1 : false
f2 : false
Assign value of f2 to f1: 
f1 : false
f2 : false
Skip f1
Skip f2