Consider a function to compare positive integers; the function itself uses a lambda to do the job ..
// Pass n1, n2 by value to the lambda. bool Compare(int n1, int n2) { return [](int n1, int n2) { return n1 > n2; }; } The above snippet compiles fine; though Compare() always returns true;
However, the following code even fails to compile -
// capturing values bool Compare(int n1, int n2) { return [n1, n2]() -> bool { return n1 > n2; }; } and returns the error
lambda.cpp:48:46: error: cannot convert 'Compare(int, int)::__lambda2' to 'bool' in return return [n1, n2]() -> bool { return n1 > n2; }; Question
May be these are not the intended use of introducing lambda's in C++, still...
- Why the first one always returns true?
- Why second fails to compile?