Lambda Expression

  • MoseHas
    Likes 0

    Problem Description

    Hi there. I hope I post this on the right area.

    I am now studying someone else’s popstar code. And this pops out. Cannot really understand even if I checked it on MSDN.

     

    He did a label floating in and out effect with such code:

    void FloatWord::floatIn(const float delay, std::function<void()> callback)
    {
    	Size visibleSize = Director::getInstance()->getVisibleSize();
    	//float in to the center of the screen. Therefore, the height is unchanged.
    	MoveTo* move = MoveTo::create(delay, Point(visibleSize.width / 2, _begin.y));
    	
    	//This part
    	CallFunc* call = CallFunc::create(callback);
    	//This part
    	
    	Sequence* action = Sequence::create(move, call, NULL);
    	_label->runAction(action);
    }

     

    I am not sure, but I think it is so called Lambda expression. It looks so abstract.

    A lambda expression is like an anonymous function that maintains state and can access the variables that are available to the enclosing scope. 

    I am not sure what it means.  Would you please explain it with an example for me?

    Thank you very much!

  • Sonar Systems admin
    Likes 0

    Do you want us to explain Lambda functions or the code?

  • MoseHas
    Likes 0

    Both, if you may. The reason why I want to understand it is because someday I might have to use it. Gotta know how it work and how to use.

    Thanks!

     

    By the way, I found a page introducing Lambda expression.

    http://en.cppreference.com/w/cpp/language/lambda

    Even the very first syntax part confuses my head. Oh my...

  • Sonar Systems admin
    Likes 0

    How familiar are you with C++?

  • MoseHas
    Likes 0

    Basically I am more familiar with C like Linked lists, stacks, pointers, etc.

    C++ part is not that good, especially polymorphism and inheritance.

  • Sonar Systems admin
    Likes 0

    Learn c++ before doing Cocos2d-x

     

    Check this out http://www.sonarlearning.co.uk/coursepage.php?topic=desktop&course=cplusplus

  • MoseHas
    Likes 0

    I have finished most of the lessons. I just picked those I am not so familiar with.

  • Sonar Systems admin
    Likes 0

    ok thats cool


    This reply has been verified.
  • MoseHas
    Likes 0

    Holy crap I have just closed the page without submitting. What a dumb I am.

     

    I have just discovered a page that explains lambda expression. Try to explain it with my words.

     

    Lambda Expression is a new function in C++11, which brings programmers flexibility and conciseness.

    It is an expression of anonymoius function, which allows the programmers create a function without declaring it. 

    [=] (int x) mutable throw() -> int
    {
      //compound-statement
      int n = x + y;
      return n;
    }

    [=]: lambda-introducer, aka capture clause.

    It is necessary to every lambda expression. Not only it is the keyword to lambda expression, but also it captures variables.

    • [ ]: Do not capture the external variables.
    • [=]: Pass every variables by value.
    • [&]: Pass every variables by reference.
    • [x, &y]: Pass x by value and pass y by reference.
    • [=, &y]: Pass y by reference and pass the rest by value.
    • [&, x]: Pass x by value and pass hte rest by reference.

    And we should put the capture-default(=,&) in the first.

     

    (int x): lambda declarator, aka parameter list.

    There are some restrictions about it.

    • No default assignment of the parameter.
    • No parameter list with dynamic size.
    • The parameter must include named parameter.

    Lambda declarator is not necessary. If we do not need to pass any parameter we could just get rid of it.

     

    mutable: mutable specification, which allows us to modify the variables passed by value. It is not necessary either.

     

    throw(): exception specification.

    (I do not know what the function exception is, but it says this could handle the exception part.)

     

    → int: return type. What lambda expression is returning. If we do not return anything, we could skip it.

     

    --

    Well, I think I get the most of it.

    Now let’s see the code and discuss it, shall we?

    void FloatWord::floatIn(const float delay, std::function callback)
    {
    	Size visibleSize = Director::getInstance()->getVisibleSize();
    	//float in to the center of the screen. Therefore, the height is unchanged.
    	MoveTo* move = MoveTo::create(delay, Point(visibleSize.width / 2, _begin.y));
    
    	//This
    	CallFunc* call = CallFunc::create(callback);
    	//This
    
    	Sequence* action = Sequence::create(move, call, NULL);
    	_label->runAction(action);
    }
    void GameLayer::floatLevelWord()
    {
    	Size visibleSize = Director::getInstance()->getVisibleSize();
    	_levelMsg = FloatWord::create("Level" + cocos2d::String::createWithFormat(": %d", GAMEDATA::getInstance()->getNextLevel())->_string, /*display level string*/
    		visibleSize.width * FONT_SIZE, /*set the font size*/
    		Point(visibleSize.width, visibleSize.height / 3 * 2)); /*set the position*/
    
    	this->addChild(_levelMsg,1);
    	_levelMsg->floatIn(FLOAT_EFFECT_DELAY_SEC, CC_CALLBACK_0(GameLayer::floatTargetScoreWord, this));
    }

    So is he passing the CC_CALLBACK_0 into the lambda function? Or did I just misunderstand?

    Thank you.

    Is this lambda expression?


  • Sonar Systems admin
    Likes 0

    If it’s not bound to an identifier then it’s a lambda function.

Login to reply