简体   繁体   English

这是? 三元运算合法吗?

[英]Is this ? ternary operation legal?

I'm no expert, but I do like to learn and understand.我不是专家,但我确实喜欢学习和理解。 With that in mind I wrote the following in the Arduino IDE:考虑到这一点,我在 Arduino IDE 中写了以下内容:

lockout[idx] ? bulb[idx].off() : bulb[idx].on();

to replace this:替换这个:

if (lockout[idx]) bulb[idx].off(); else bulb[idx].on();

lockout[] is an array of bool , and bulb[] is an array of a class, with. lockout[]是一个bool数组, bulb[]是一个 class 数组,带有。 off and .on methods. off.on方法。

I've looked around for examples and never seen this usage of the ?我环顾四周寻找示例,但从未见过? ternary operator.三元运算符。 And what I've read seems to say that this should not work.我读过的似乎是说这不应该奏效。

But it does compile.但它确实编译。 So is this in fact legitimate C++?那么这实际上是合法的 C++ 吗?

Yes, this is legitimate C++.是的,这是合法的 C++。 While that operator is commonly called the ternary operator , it is called the conditional operator in the C++ standard, and it is defined in the section named "expr.cond".虽然该运算符通常称为三元运算符,但在 C++ 标准中称为条件运算符,并在名为“expr.cond”的部分中定义。

The C++ standard explicity says it is OK for both the second and third operands to have type void . C++ 标准明确表示第二个和第三个操作数都可以具有类型void So the standard writers knew that people might want to use this operator as a short way to write if statements, like you are doing.因此,标准编写者知道人们可能希望使用此运算符作为编写if语句的简短方法,就像您正在做的那样。

If the types of the second or third operands are not void , then the standard says "an attempt is made to convert each of those operands to the type of the other" and it goes into detail about what that means.如果第二个或第三个操作数的类型不是void ,那么标准会说“尝试将这些操作数中的每一个转换为另一个的类型”,并详细说明这意味着什么。

For reference, the version of the C++ standard I am referring to is N4296, so it's a little old but I don't think that matters.作为参考,我指的C++标准的版本是N4296,所以有点旧,但我认为这并不重要。

If it compiles, it is probably well-formed code.如果它编译,它可能是格式良好的代码。 The compiler would issue an error message if the ternary operator could not be used with those operands on account of their type.如果三元运算符由于其类型而无法与这些操作数一起使用,编译器将发出错误消息。 The fact that there's no such error message suggests that there is no such problem with your code.没有此类错误消息的事实表明您的代码不存在此类问题。

However, under some conditions, use of the ternary operator can result in possibly unintended copying.但是,在某些情况下,使用三元运算符可能会导致意外复制。

If both the on and off methods return void (or, say, bool ) there won't be any issue of copying, and the code will work as expected: if lockout[idx] is true then bulb[idx].off() will be evaluated, otherwise bulb[idx].on() will be evaluated.如果onoff方法都返回void (或者说, bool ),则不会有任何复制问题,并且代码将按预期工作:如果lockout[idx]为真,则bulb[idx].off()将被评估,否则bulb[idx].on()将被评估。 Only one of the two alternatives will be evaluated, just like what would happen with the if statement.将只评估两个备选方案中的一个,就像if语句会发生的情况一样。

Generally, the ternary operator is used when you need the result to be an expression.通常,当您需要将结果作为表达式时,会使用三元运算符。 Otherwise, writing the code using an if statement is usually more readable.否则,使用if语句编写代码通常更具可读性。

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

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