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.