fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. int foo( int n, std::function< void(int) > notify )
  5. {
  6. int sum = 0 ;
  7. for( int i = 1 ; i < n ; ++i )
  8. {
  9. sum += i*i ;
  10. notify(sum) ;
  11. }
  12. return sum ;
  13. }
  14.  
  15. int main()
  16. {
  17. const auto report_progress = [] ( int s )
  18. { std::cout << "intermediate value returned: " << s << '\n' ; };
  19. foo( 10, report_progress ) ;
  20. }
  21.  
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
intermediate value returned: 1
intermediate value returned: 5
intermediate value returned: 14
intermediate value returned: 30
intermediate value returned: 55
intermediate value returned: 91
intermediate value returned: 140
intermediate value returned: 204
intermediate value returned: 285