Thread: C++: lambdas - what is the best for inicializate the lambda?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    451

    C++: lambdas - what is the best for inicializate the lambda?

    Code:
    #define event(EventName, ... ) std::function<void(__VA_ARGS__ )> EventName
    if i do:
    Code:
    event(Create) =NULL;
    on form procedure:
    Code:
    static LRESULT CALLBACK WndProcForm(HWND HandleWindow, UINT msg, WPARAM wParam, LPARAM lParam)
            {
                static bool blnInstCreated=false;
                static POINT PreviousLocation, Location;
                static bool Tracking = false;
                static MouseButtons MBButtons=MouseButtons::None;
                static bool blControl = false;
                static bool blShift = false;
                static bool blResize = false;
                static int xPos = 0;
                static int yPos = 0;
                const UINT_PTR timerid=0;
                const UINT_PTR KeyBoardTimer=2;
                const UINT_PTR JoystickTimer=1;
                static HMENU menuhandle=NULL;
                static bool blnDrag = false;
                static bool KeyPressed=false;
                static bool JoystickIsConneted=false;
                static bool blnMouseDown=false;//testing the mouse down
    
    
    
                form *inst = (form *)GetWindowLongPtr(HandleWindow, GWLP_USERDATA);
    
                if(inst!=NULL && inst->Create )
                {
                    static bool i=true;
                     if(i==true)
                     {
                        blnInstCreated=true;
                        i=false;
                        inst -> Create();
                     }
                }
    the Create is called.
    but if i do:
    Code:
    event(Create) =[](){;};
    the Create isn't called... why?
    (i use a macro for be more easy)

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Don't use a macro, use auto.
    Code:
        auto f = [](){ cout << "hello world" << endl; };
        f();
    Super easy.

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Were you trying to initialize a std::function with a lambda? I guess you could do that but lambdas are a compiler-generated type so as whiteflags said, just use auto because the actual type of a lambda is only of concern to the compiler.

    I think C++17 is implementing std::is_callable as well so you can build some pretty nice looking templated code.

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    but i'm asking 1 thing for an 'empty' lambda... it's:
    Code:
    [](){;}
    or
    Code:
    [](){}
    or
    Code:
    NULL
    ?????

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Define an "empty" lambda. A lambda is a callable object. Being empty could mean something such as a non-existent object or a lambda that has an empty body.

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by MutantJohn View Post
    Define an "empty" lambda. A lambda is a callable object. Being empty could mean something such as a non-existent object or a lambda that has an empty body.
    i'm sorry, but from that 3 ways, what is the more correct?

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    My comments on your code are simple.
    Code:
    
    [](){} and [](){;}
    should be the same, because the ; statement is a no-op.

    What isn't the same is NULL. From what I can tell that originally worked for you because std::function has a nullptr_t constructor:
    Code:
    function( std::nullptr_t );

  8. #8
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    thanks for all to all... thank you

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by joaquim View Post
    Code:
    NULL
    Don't use NULL. Use nullptr. nullptr is guaranteed to be a pointer, and will not accidentally convert to int.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  10. #10
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by Elkvis View Post
    Don't use NULL. Use nullptr. nullptr is guaranteed to be a pointer, and will not accidentally convert to int.
    thank you for correct me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. know if T is a lambda
    By Dave11 in forum C++ Programming
    Replies: 8
    Last Post: 06-27-2015, 04:27 PM
  2. Templates, lambdas and overloaded operators
    By nonpuz in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2015, 09:09 PM
  3. Lambdas, why are they so confusing?
    By MutantJohn in forum C++ Programming
    Replies: 16
    Last Post: 09-28-2013, 07:10 AM
  4. GCC 4.5 Lambdas
    By Dae in forum C++ Programming
    Replies: 0
    Last Post: 11-23-2009, 03:20 PM
  5. Qt and C++0x lambdas: they don't seem to mix
    By Zach_the_Lizard in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2009, 05:57 AM

Tags for this Thread