I want to find point, which has the less Y coordinate (if more of such points, find the one with smallest X). When writing it with lambda:
std::min_element(begin, end, [](PointAndAngle& p1, PointAndAngle& p2) { if (p1.first->y() < p2.first->y()) return true; else if (p1.first->y() > p2.first->y()) return false; else return p1.first->x() < p2.first->x(); }
I am getting:
error C3499: a lambda that has been specified to have a void return type cannot return a value
what is the difference between:
// works std::min_element(begin, end, [](PointAndAngle& p1, PointAndAngle& p2) { return p1.first->y() < p2.first->y(); }
and
// does not work std::min_element(begin, end, [](PointAndAngle& p1, PointAndAngle& p2) { if (p1.first->y() < p2.first->y()) return true; else return false; }
As Mike noted, if the lambda's body is a single return statement, then the return type is inferred from that (see 5.1.2/4) (thanks Mike).
std::min_element(begin, end, [] (const PointAndAngle & p1, const PointAndAngle & p2) -> bool { if (p1.first->y() < p2.first->y()) return true; else return false; }
Note -> bool
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With