I'm playing with C++11 for fun. I'm wondering why this happens:
//...
std::vector<P_EndPoint> agents;
P_CommunicationProtocol requestPacket;
//...
bool repeated = std::any_of(agents.begin(), agents.end(),
[](P_EndPoint i)->bool
{return requestPacket.identity().id()==i.id();});
Compilation terminates with this error:
error: 'requestPacket' has not been declared
Which is declared earlier in code. I tried ::requestPacke
and it doesn't worked too.
How can I use an external scope variable inside a lambda function?
You need to capture the variable, either by value (using the
[=]
syntax)or by reference (using the
[&]
syntax)Note that as @aschepler points out, global variables with static storage duration are not captured, only function-level variables: