简体   繁体   English

Lambda表达式在if语句中返回bool

[英]Lambda expression to return bool in if statement

Just to get to the point, I want to return true or false using the lambda expression inside the if() statement. 为了达到目的,我想使用if()语句中的lambda表达式返回truefalse I saw this question which has similar question to mine: LINK but I could not find the answer. 我看到这个问题与我有类似的问题: LINK但我找不到答案。

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. 好的,读错误我认为我没有调用函数,因为编译器不会将表达式视为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. 我知道在这种特殊情况下lambda表达没有任何意义。

you forgot to call your lambda. 你忘了叫你的lambda。 Right now you're saying if(function_pointer) hence the compiler failing to convert that into a boolean expression. 现在你说if(function_pointer)因此编译器无法将其转换为布尔表达式。


A simple if clause with boolean lambda is therefore written like: 因此,使用布尔lambda的简单if子句如下所示:

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. 在这种情况下,lambda的需要是有问题的,你可以摆脱lamdba,在if子句中留下布尔表达式。 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. 足够且更具可读性。

A lambda is a function object. lambda是一个函数对象。 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. 您将rel_pose作为参数传递而不是捕获它。 You should probably make pose a const reference anyway. 无论如何你应该使pose成为const引用。

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) { /* ... */ }

this is defining a lambda with capture phrase, not calling lambda 这是用捕获短语定义一个lambda,而不是调用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, 如果你想用rel_pose作为参数调用lambda,

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

i think this is right 我认为这是对的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM