fork download
  1. #include <functional>
  2. #include <vector>
  3. #include <iostream>
  4.  
  5. using std::cout;
  6. using std::endl;
  7.  
  8. struct S {
  9. std::function< void() > func;
  10. //S( std::function< void() > && f ) : func( f ) {}
  11. };
  12.  
  13. void foo( const S & s ) {
  14. if ( s.func ) {
  15. cout << "ok : ";
  16. s.func();
  17. }
  18. else {
  19. cout << "fail" << endl;
  20. }
  21. }
  22.  
  23. void bar_i( int i ) { cout << i << endl; }
  24. void bar( std::vector< int > i ) { cout << i[ 0 ] << endl; }
  25.  
  26. int main() {
  27. foo( S{ std::bind( bar_i, 1 ) } );
  28. S s_i{ std::bind( bar_i, 2 ) };
  29. foo( s_i );
  30.  
  31. std::vector< int > one = { 42 };
  32. foo( S{ std::bind( bar, one ) } );
  33. S s{ std::bind( bar, one ) };
  34. foo( s );
  35.  
  36. }
  37.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
ok : 1
ok : 2
ok : 42
ok : 42