What's the difference between

[&](){x;} 

and

 [&x](){x;} 

?

  • one
    For example, if x is a macro, then the difference can be arbitrarily big :) - VladD
  • @VladD The question is whether there is a difference between the fact that the lambda immediately knows the variables to be captured or from {} retrieves them - dfa
  • one
    @dfa Well, regarding your question, the difference is that in the second case you explicitly limit the list of variables visible in the function declaration area (in the context of which lambda is declared) that you can capture (for example, to severely restrict yourself from using excess inside the lambda), and in the first case, all the variables used inside the lambda from the function declaration area are automatically snatched away. - StateItPrimitive
  • @StateItPrimitive well, this is understandable, but from the point of view of capture, "explicitly" and "implicitly." Mb when it is determined itself is slower? - dfa
  • 2
    @dfa I assume that the list of lamba capture parameters is determined at the time of compilation (i.e., when the code is translated into object files), i.e. cannot influence runtime in any way. - StateItPrimitive 5:42 pm

1 answer 1

In this particular embodiment, essentially nothing. Just in the first case, the capture of all variables by reference occurs, and in the second, the specific one. But since nothing in the body except x is used, there is no difference. Especially when optimizing the code :) - they will most likely just be removed altogether.

VC ++ 2015 did exactly that - threw it away at all during optimization, and without it, generated two absolutely identical codes.