0

i'm new to using lambda in cpp. Can anyone elaborate how lambda is getting called in this case particularly. As a matter of fact i got this as a solution to better understand lambda by my prof.

std::function<int(int)> ptr=[](int num)->int {return num+100;};
int rval=ptr(10);
cout<<rval;
  • 4
    fwiw, this isnt a good example. The `std::function` is unnecessary here and just adds complexity. – 463035818_is_not_an_ai May 06 '20 at 12:41
  • 2
    Did you read this: https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11 ? – 463035818_is_not_an_ai May 06 '20 at 12:41
  • how std::function is accepting when i didn't even called lambda. i read how function works,but not able to understand this. – Roushan Kumar May 06 '20 at 12:52
  • please edit your question to include a specific question. If you don't understand ``, that could make a specific question, but then the lambda is only a secondary issue, and currently it isnt clear that it is `` what your question is about – 463035818_is_not_an_ai May 06 '20 at 12:54
  • 2
    `std::function` is a functor that accepts int as a parameter and returns int. This lambda `[](int) -> int {...}` is a functor that accepts int as a parameter and returns int. The lambda can be assigned to the `std::function` just fine. You are calling it when you do `ptr(10)`.. You can also do `auto ptr = [](int num) -> int { ... }` and it'd still work without using `std::function`. – Brandon May 06 '20 at 13:20
  • "i didn't even called lambda" yes you did, you called it through `ptr` – Caleth May 06 '20 at 15:18

1 Answers1

0

The last answer by @Brandon is correct. Here is the long version:

I think the MSDN site has the best explanation of how to parse a lambda. Please see: https://learn.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp

I'm going to re-write your professor's answer with some whitespace for readability:

    std::function<int(int)> ptr = [] (int num) -> int {return num + 100;};

Let's parse it according to the MSDN recipe:

1. [] the capture clause: nothing from the caller is being captured. Why would you use a list of captured items instead of placing the same items into the parameter list? This is a very nice SO answer. In the example case, nothing is captured since you are just performing an operation on a passed in parameter.

2. The parameter list: A single int. This should be familiar. A function taking a parameter int.

5. The return type: I think it is a good idea to provide the return type specification, especially when the type is a reference. I have been bit by this before. Often the return type is merely implied by the signature of the function.

6. The lambda body: { return num + 100; } Just like a regular function. This is a simple example, and often lambdas are small in textual length. But this could be very complex.

Finally, the lambda type is returned as std::function<> type. Most people will use auto here. Likely your professor is trying to help by showing you the types involved.

Well, I just made a short story long. I suggest reading that SO link to the other lambda question and reading the MSDN doc. The nice thing about the MSDN page is that it avoids using words like 'predicate' and lays everything out in a diagram.

natersoz
  • 1,674
  • 2
  • 19
  • 29
  • 2
    `std::function` isnt really "showing the types involved" it rather adds more. The lambda is of a unique unnamed type and `std::function` is a different type – 463035818_is_not_an_ai May 06 '20 at 14:56