Why does C ++ 11 and closing PHP require declaring private variables?

Functional literals in both C ++ and PHP require the programmer to indicate which variables they use from the current lexical context. What is the reason for this requirement?

I assume that this is not intended for the compiler / interpreter, because you can statically output this information from the body of the function. Is this just to get the reader's attention?

+7
source share
2 answers

For C ++ 11, at least [=] () {...} will automatically pull out all and only those local variables that the function body uses. (Or, equally, [&]... )

You can specify individual variables that should be written by reference or by value if you have any specific needs that go beyond this catch-all.

In PHP, variables are created when their name is first used, so I expect the declaration to make sure that new variables do not mask old ones. A bit like the global .

+10
source

I can’t say for php, and I didn’t get your question 100%, but ... in C ++, the variable total partakes is in closing the lambda function. the basic premise is that it can change its value as a link.

0
source

All Articles