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



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

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

    What does it mean? [&] before the function?



  • This isn't exactly a function. Technically it is, but it is an anonymous function, you seem to know about it, most will not even see it as a function.

    It means that all variables captured by this possible https://pt.stackoverflow.com/q/34907/101 will be captured as references. This means that their values will not be copied to their function lick, but only a reference for them will exist within the function and obviously these variables will need to survive at least while their lick exist.

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

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

    Note that I'm not talking about the parameters, I'm talking about possible variables used within its function that have not been declared within it (parameters are local to it), and obviously have been declared in the function where this lick was written or in a previous scope, but they must be in https://pt.stackoverflow.com/q/135572/101 . I can't tell if there is any because her body was omitted (and ideally all the 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 her:

    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 (e.g.) inside would not be a clausura and therefore there would be no capture of variables and would make no difference how capture was declared.

    If no variable is captured by not using any declared outside the scope of lick so 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 https://ideone.com/6S36fF . And https://repl.it/join/mfgzcklw-maniero . Also https://github.com/maniero/SOpt/blob/master/CPP/Syntax/Closure.cpp .

    In C++ it is possible to say however each variable is captured, but the most common is to use the standard or all by reference. But you'll always have to take care of the variable's life time, and that's one of the most complicated things if it's not for value, which in some cases makes you miss one garbage collector.

    https://en.cppreference.com/w/cpp/language/lambda .




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2