none
how to pass argument to std::function?

    Question

  • experimenting with c++ 11 lambdas ...

    How to add a parameter to the std::function in this example?  ( and how to call such a function with arguments? )

      function<void()> run = []
      {  
        for ( long c = 0 ; c < 100 ; ++c )
          nerdscentral::performRun() ; 
      };
    

    I want to use similar code that will run on another thread. I am hoping I can use parameters to the lambda function to pass variables to the background thread.

      std::function<void()> run = [](HWND hIn )
      {
        ::Sleep(10000) ;
        ::PostMessage(hIn, WM_CLOSE, 0, 0) ;
      };
    

    thanks,

    lundi 16 avril 2012 16:35

Réponses

  • chekc this link this may help you out

    http://stackoverflow.com/questions/7909719/c0x-stdfunction-as-a-method-argument

    lundi 16 avril 2012 17:23
  • On 4/16/2012 12:35 PM, Steve Richter wrote:

    How to add a parameter to the std::function in this example? ( and how to call such a function with arguments? )

         function<void()>    run = []
         {
             for ( long c = 0 ; c<    100 ; ++c )
                 nerdscentral::performRun() ;
         };
    
        function<void(long)> run = [](long bound)
        {
            for ( long c = 0 ; c < bound ; ++c )
                nerdscentral::performRun() ;
        };
    
    run(100);

    Igor Tandetnik

    lundi 16 avril 2012 17:46

Toutes les réponses

  • chekc this link this may help you out

    http://stackoverflow.com/questions/7909719/c0x-stdfunction-as-a-method-argument

    lundi 16 avril 2012 17:23
  • chekc this link this may help you out

    http://stackoverflow.com/questions/7909719/c0x-stdfunction-as-a-method-argument

    thank you. I need examples.  This stuff is too dense for me.

    the following code does not show a syntax error in vs11, but compiles with an error C2064: term does not evaluate to a function taking 1 arguments.

      std::function<void(HWND)> run = [](HWND hIn )
      {
        ::Sleep(10000) ;
        ::PostMessage(hIn, WM_CLOSE, 0, 0) ;
      };
    
    Error	1	error C2064: term does not evaluate to a function taking 1 arguments	C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xrefwrap	96	1	HookDllTester
    
    _VARIADIC_EXPAND_0X(_CLASS_RESULT_TYPE, , , , )
    

    lundi 16 avril 2012 17:43
  • On 4/16/2012 12:35 PM, Steve Richter wrote:

    How to add a parameter to the std::function in this example? ( and how to call such a function with arguments? )

         function<void()>    run = []
         {
             for ( long c = 0 ; c<    100 ; ++c )
                 nerdscentral::performRun() ;
         };
    
        function<void(long)> run = [](long bound)
        {
            for ( long c = 0 ; c < bound ; ++c )
                nerdscentral::performRun() ;
        };
    
    run(100);

    Igor Tandetnik

    lundi 16 avril 2012 17:46