Thread: Arguments of lambda functions need to be local?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    106

    Arguments of lambda functions need to be local?

    Hello,

    I just wanted to check on this. I have the statement below inside a function (method of Player) that checks that all weapons in the Loadout vector has the same scale as the Player who carries them:

    Code:
    int playerscale = PlayerScale;
    bool IncorrectScale = any_of(Loadout.begin(), Loadout.end(), [&playerscale](auto& weapon) { return weapon->WeaponScale != playerscale; });
    My question is: Does PlayerScale always need to be converted to local before being used this way? Thank you for your time!

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    814
    You can capture (by value) the pointer to your class object:
    Code:
    bool IncorrectScale = any_of(Loadout.begin(), Loadout.end(), [this](auto& weapon) { return weapon->WeaponScale != PlayerScale; });

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    106
    Quote Originally Posted by -Adrian View Post
    You can capture (by value) the pointer to your class object:
    Code:
    bool IncorrectScale = any_of(Loadout.begin(), Loadout.end(), [this](auto& weapon) { return weapon->WeaponScale != PlayerScale; });
    Ah.. nice! Thx again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use local variables in other functions
    By HassSugma in forum C Programming
    Replies: 3
    Last Post: 09-21-2019, 02:51 AM
  2. C++11 : Lambda functions return value
    By Alexandre in forum C++ Programming
    Replies: 6
    Last Post: 09-14-2011, 01:24 PM
  3. Lambda functions in C?
    By black0ut in forum C Programming
    Replies: 2
    Last Post: 08-12-2009, 01:26 PM
  4. Where to put local auxiliary functions?
    By draugr in forum C++ Programming
    Replies: 10
    Last Post: 03-17-2009, 08:46 PM
  5. local IP identification via C internal functions
    By andrew in forum C Programming
    Replies: 0
    Last Post: 12-19-2001, 06:46 AM

Tags for this Thread