Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lambda returning bool

Tags:

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;     } 
like image 249
relaxxx Avatar asked Oct 25 '11 12:10

relaxxx


1 Answers

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.

like image 155
sehe Avatar answered Sep 29 '22 05:09

sehe