0

im working on a C implementation of a library originally written in C++, in a section the original developer uses this sentence:

[&value](const T& item) { value = item; };

what does mean this use of keys, parentheses is some kind of cast? below the funcion code. thanks for the help.

    /**
        Retrieve the only value from the collection.
    */
    bool ReadOnlyValue(T& value) const
    {
        if (this->Count() == 1)
        {
            auto assignValue = [&value](const T& item) { value = item; };
            this->ForeachItem(assignValue);
            return true;
        }
        else
        {
            return false;
        }
    } 
  • 6
    It is a [lambda](https://en.cppreference.com/w/cpp/language/lambda). – Jarod42 Jan 13 '22 at 16:07
  • `assignValue` is a lambda (kind of a function), that captures `value` by reference (to use it in its body), and receives an `item` by const reference, and assigns `item` to `value`. – rturrado Jan 13 '22 at 16:08
  • 2
    Related: [What is a lambda expression in C++11?](https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11) – jodag Jan 13 '22 at 16:09

0 Answers0