What does the [&] operator mean before a function?

I am trying to read the code of a function that is defined like this:

auto loop = [&](int ntensor, char** data, const int64_t* strides, int64_t n) {
...
  };

What Does [&] mean before the function?

Author: Maniero, 2019-02-10

1 answers

This is not quite a function. Technically it is, but it is an anonymous function, you seem to know roughly, most will not even see it as a function.

Means that all variables captured by this possible closure will be captured as references. This means that their values will not be copied to your function lambda, but only a reference to them will exist within the function and obviously these variables will need to survive for the first time. less as long as your lambda exists.

This sets a pattern for all captured variables unless any are specified differently.

This is necessary because in catches we do not always want the value in the same way as they were declared.

Note that I'm not talking about the parameters, I'm talking about possible variables used within your function that have not been declared within it (parameters are local to it), and obviously have been declared in the function where this lambda was written or in previous scope, but they must be in the scope . I can not say if there is one because precisely her body was omitted (and ideally the whole context would show better). Something like this:

void Teste() {
    int i = 1;
    auto loop = [&](int ntensor, char** data, const int64_t* strides, int64_t n) {
        ...
        //faz algo com i aqui e este será uma referência para o i declarado antes
        ...
    };
    //faz algo com loop aqui, possivelmente passar como argumento para outra função
}

There will already be a copy of the value of i and no reference to it:

void Teste() {
    int i = 1;
    auto loop = [=](int ntensor, char** data, const int64_t* strides, int64_t n) {
        ...
        //faz algo com i aqui (neste exemplo eu sei que é 1, poderia ser outro valor)
        ...
    };
    //faz algo com loop aqui, possivelmente passar como argumento para outra função
}

If I didn't have that i (for example) inside it wouldn't be a closure and so there would be no variable capture and no it would make a difference how the capture was declared.

If no variable is captured for not using any declared outside the scope of lambda then it doesn't change anything.

A functional example:

#include <iostream>
using namespace std;

int main() {
    int i = 1;
    auto loop = [=]() { cout << i; }; //não pode fazer o incremento aqui
    loop();
    auto loop2 = [&]() { cout << i++; };
    loop2();
    cout << i;
}

See working on ideone. E no repl.it. also I put on GitHub for future reference .

In C++ it is possible to say how you want each variable to be captured, but the most common is to use the default or all by reference. But you'll always have to take care of the variable's lifetime, and that's one of the trickiest things if it's not by value, which makes in some cases you miss a garbage collector.

Documentation .

 8
Author: Maniero, 2020-08-27 18:11:58