fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. using namespace std::placeholders;
  5.  
  6. class DelegateToFunctionEnd {
  7.  
  8. typedef std::function<void(void)> EndFunction;
  9.  
  10. public:
  11. DelegateToFunctionEnd(EndFunction endFunction) :
  12. callAtEnd(endFunction)
  13. { }
  14.  
  15. ~DelegateToFunctionEnd() {
  16. callAtEnd();
  17. }
  18.  
  19. private:
  20.  
  21. EndFunction callAtEnd;
  22. };
  23.  
  24. class Results {
  25.  
  26. public:
  27.  
  28. void setStatus(int i) {
  29. status = i;
  30. }
  31.  
  32. int status;
  33. };
  34.  
  35.  
  36. int function(Results &res) {
  37. int error;
  38. DelegateToFunctionEnd del(std::bind(&Results::setStatus, &res, std::cref(error)));
  39. // Some operations
  40. error = -10;
  41. if (error < 0) {
  42. return 0;
  43. }
  44.  
  45. }
  46.  
  47. int main() {
  48.  
  49. Results r;
  50. function(r);
  51.  
  52. std::cout << r.status;
  53.  
  54. // your code goes here
  55. return 0;
  56. }
Success #stdin #stdout 0s 4532KB
stdin
Standard input is empty
stdout
-10