Search code examples
c++functionpointersreturnstructure

Not able to understand the meaning of this code


I dont understand what the following code is doing

auto sgn = [&](int x) 
{
        if (x > 0) return 1;
        else return -1;
};

What is the role of ampersand here? Is it a pointer? And is this block a structure or something like that?

I came across this block of code in codeforces round 636 division 3 editorial.


Solution

  • The & here means that the lambda captures all variables by reference.

    The lambda returns 1 or -1 depending on the value of the argument x, so the capture really doesn't actually matter here.