Does lambda functions changes to accept default arguments in c++ 14?
This example doesn't work on C++ 11:
int main() {
int i = 34;
auto RectSurf = [i](int length = 0, int width = 0){ return length * width;};
cout << RectSurf(10) << endl;
std::cout << std::endl;
}
But it works fine on C++ 14 and above. So is it correct or not? Because C++ prime 5ed said that: "Passing Arguments to a Lambda As with an ordinary function call, the arguments in a call to a lambda are used to initialize the lambda’s parameters. As usual, the argument and parameter types must match. Unlike ordinary functions, a lambda may not have default arguments (§ 6.5.1, p. 236). Therefore, a call to a lambda always has as many arguments as the lambda has parameters. Once the parameters are initialized, the function body executes.
As an example of a lambda that takes arguments, we can write a lambda that behaves like our isShorter function:" From C++ Prime 5ed.
But the same code on C++14 and above works fine! But Also I've read here in S.O That lambda functions can have a default parameter on condition that no variable has been captured in Lambda's capture list.
Aucun commentaire:
Enregistrer un commentaire