Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does [&] mean before function? [duplicate]

Tags:

c++

What does the following code mean?

auto allowed = [&](int x, const std::vector<int>&vect){  ....  } 

I mean, what does [&] do? Is it a function with the same name of the variable?

Because it is used in this way: unsigned short ok = get_allowed(0, vect);

like image 772
Droide Avatar asked Sep 30 '16 10:09

Droide


People also ask

What does the fox say Meaning?

Speaking of the meaning of the song, Vegard characterizes it as coming from "a genuine wonder of what the fox says, because we didn't know". Although interpreted by some commentators as a reference to the furry fandom, the brothers have stated they did not know about its existence when producing "The Fox".

What does a real fox say?

One of the most common fox vocalizations is a raspy bark. Scientists believe foxes use this barking sound to identify themselves and communicate with other foxes. Another eerie fox vocalization is a type of high-pitched howl that's almost like a scream.


1 Answers

It means that the lambda function will capture all variables in the scope by reference.

To use other variables other than what was passed to lambda within it, we can use capture-clause []. You can capture by both reference and value, which you can specify using & and = respectively:

  • [=] capture all variables within scope by value
  • [&] capture all variables within scope by reference
  • [&var] capture var by reference
  • [&, var] specify that the default way of capturing is by reference and we want to capture var
  • [=, &var] capture the variables in scope by value by default, but capture var using reference instead
like image 127
nishantsingh Avatar answered Sep 20 '22 01:09

nishantsingh