First example:
struct State
{
SomeLargeObjectThatTakesTimeToCopy obj;
int x;
} myState;
auto f = [=]() { return myState.x * 2; };
Does the entire myState struct get copied, even though, technically, only the x member is used?
Second example:
struct State
{
struct SubState
{
int x;
};
std::vector<SubState> subStates;
} myState;
auto f = [=]() { return myState.subStates[0].x * 2; };
Again, does the entire myState object get copied? If not, then does the entire subStates member get copied?
Assuming
myStateis a local variable (defined in the body of a function),myStatewould get captured; lambda can only capture whole variables, not bits and pieces.If you take your examples as-is, nothing gets captured; lambdas can’t capture global variables.