将 lambda 传递给三元运算符的第二个子句
Pass a lambda into the second clause of a ternary operator
我正在尝试让三元运算符与 lambda 一起工作。以下是我想转换成三元运算的逻辑
if (boolean_condition_var == false) {
RandomMethod();
}
// From here some logic to execute
int i = 0;
// blah blah logic
以下是我在通读 this link.
后尝试使其工作的方式
boolean_condition_var == false ? RandomMethod() : std::function<void(void)>([this](){
// execute some logic here
int i = 0;
//blah blah logic
});
但我收到以下错误:
问题:
error: left operand to ? is void, but right operand is of type 'std::function<void ()>'
boolean_condition_var == false ? RandomMethod() : std::function<void(void)>([this](){ int i = 0; });
问题:
如何将 lambda 传递到三元运算符的第二个子句中?
How can I pass a lambda into the second clause of my ternary operator?
使用逗号运算符 ?
boolean_condition_var == false
? (RandomMethod(), 0)
: (std::function<void(void)>([this](){ /*...*/ })(), 0);
或者,我想更好,
std::function<void(void)> logicElse { [this](){ /*...*/ } };
boolean_condition_var == false
? (RandomMethod(), 0)
: (logicElse(), 0);
由于您没有调用 lambda,因此创建它没有什么意义。你要做的相当于
boolean_condition_var ? void(0) : RandomMethod();
另一方面,如果要调用 lambda,请使用调用运算符:
boolean_condition_var? ([this](){...your code...}) ( ) : RandomMethod();
// ^
// |
//--------------------------------------------------+
无需将任何内容转换为 std::function
。
这里更欢迎使用普通的旧式 if-then-else 语句。
另一方面,如果您希望您的三元运算符 return 可调用,您可能需要类似
using voidf = std::function<void(void)>;
auto result = boolean_condition_var?
voidf([this](){...your code...}):
voidf([this](){RandomNethod();});
.... result();
按值捕获 boolean_condition_var
并将条件移动到(现在是单个的)lambda 中可能更易读且更有效。
我正在尝试让三元运算符与 lambda 一起工作。以下是我想转换成三元运算的逻辑
if (boolean_condition_var == false) {
RandomMethod();
}
// From here some logic to execute
int i = 0;
// blah blah logic
以下是我在通读 this link.
后尝试使其工作的方式boolean_condition_var == false ? RandomMethod() : std::function<void(void)>([this](){
// execute some logic here
int i = 0;
//blah blah logic
});
但我收到以下错误:
问题:
error: left operand to ? is void, but right operand is of type 'std::function<void ()>'
boolean_condition_var == false ? RandomMethod() : std::function<void(void)>([this](){ int i = 0; });
问题:
如何将 lambda 传递到三元运算符的第二个子句中?
How can I pass a lambda into the second clause of my ternary operator?
使用逗号运算符 ?
boolean_condition_var == false
? (RandomMethod(), 0)
: (std::function<void(void)>([this](){ /*...*/ })(), 0);
或者,我想更好,
std::function<void(void)> logicElse { [this](){ /*...*/ } };
boolean_condition_var == false
? (RandomMethod(), 0)
: (logicElse(), 0);
由于您没有调用 lambda,因此创建它没有什么意义。你要做的相当于
boolean_condition_var ? void(0) : RandomMethod();
另一方面,如果要调用 lambda,请使用调用运算符:
boolean_condition_var? ([this](){...your code...}) ( ) : RandomMethod();
// ^
// |
//--------------------------------------------------+
无需将任何内容转换为 std::function
。
这里更欢迎使用普通的旧式 if-then-else 语句。
另一方面,如果您希望您的三元运算符 return 可调用,您可能需要类似
using voidf = std::function<void(void)>;
auto result = boolean_condition_var?
voidf([this](){...your code...}):
voidf([this](){RandomNethod();});
.... result();
按值捕获 boolean_condition_var
并将条件移动到(现在是单个的)lambda 中可能更易读且更有效。