Lambda expression to return bool in if statement

爷,独闯天下 提交于 2019-12-10 15:15:20

问题


Just to get to the point, I want to return true or false using the lambda expression inside the if() statement. I saw this question which has similar question to mine: LINK but I could not find the answer.

So here is my example code:

if([&rel_pose](Eigen::VectorXd pose)
    {
        return (sqrt(pose(0) * pose(0) + pose(1) * pose(1)) < 2) ? true : false;
    }) // do smth

When I try to compile I get this error:

 error: could not convert ‘<lambda closure object>graphslam::GraphSLAM::main_pose_callback(const ConstPtr&)::<lambda(Eigen::VectorXd)>{rel_pose}’ from ‘graphslam::GraphSLAM::main_pose_callback(const ConstPtr&)::<lambda(Eigen::VectorXd)>’ to ‘bool’
  })

Ok, reading the error I thought that I did not call the function as the compiler does not treat the expression as bool. So I tried to use this code:

if(([&rel_pose](Eigen::VectorXd pose)
    {
        return (sqrt(pose(0) * pose(0) + pose(1) * pose(1)) < 2) ? true : false;
    };)) // do smth

And the error:

expected ‘)’ before ‘;’ token
  };)) return;

That might look like an obvious error but for me, I probably do not understand the syntax correctly and thought to ask what is happening.

EDIT: Please note that I have simplified the code so you can replicate the error easily. I know that lambda expression in this particular case does not make any sense.


回答1:


you forgot to call your lambda. Right now you're saying if(function_pointer) hence the compiler failing to convert that into a boolean expression.


A simple if clause with boolean lambda is therefore written like:

if ([]() {
    return true;
}()) {
    //do sth
}

You also have an error by having a variable be a parameter while simultaneously capturing it. You have to decide, so either:

if([](Eigen::VectorXd pose)
    {
        return (sqrt(pose(0) * pose(0) + pose(1) * pose(1)) < 2) ? true : false;
    }(rel_pose)){
    //do sth
}

or

if([&rel_pose]()
    {
        return (sqrt(rel_pose(0) * rel_pose(0) + rel_pose(1) * rel_pose(1)) < 2) ? true : false;
    }()){
    //do sth
}

The need of a lambda is in this case questionable, you could just get rid of the lamdba leaving the boolean expression in the if clause. When talking about it - no need to use a ternary operator here. return sqrt(rel_pose(0) * rel_pose(0) + rel_pose(1) * rel_pose(1)) < 2; is sufficient and more readable.




回答2:


this is defining a lambda with capture phrase, not calling lambda

[&rel_pose](Eigen::VectorXd pose)
{
  return (sqrt(pose(0) * pose(0) + pose(1) * pose(1)) < 2) ? true : false;
}

if you want to call lambda with rel_pose as argument,

[](Eigen::VectorXd pose)
{
  return (sqrt(pose(0) * pose(0) + pose(1) * pose(1)) < 2) ? true : false;
}(rel_pose)

i think this is right




回答3:


A lambda is a function object. You should invoke it with ().

if ([](Eigen::VectorXd pose)
    {
        return (sqrt(pose(0) * pose(0) + pose(1) * pose(1)) < 2) ? true : false;
    }(rel_pose)) { /* ... */ }

You pass rel_pose as an argument instead of capturing it. You should probably make pose a const reference anyway.

That said, I don't know what you are trying to do here. This would be better:

if (sqrt(foo(0) * foo(0) + foo(1) * foo(1)) < 2) { /* ... */ }


来源:https://stackoverflow.com/questions/56288821/lambda-expression-to-return-bool-in-if-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!