c ++三元运算符 [英] c++ ternary operator

查看:193
本文介绍了c ++三元运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遇到了一些有趣的事情,我没有意识到三元运算符(至少在Visual C ++ 98-2010)。如 http://msdn.microsoft.com/en-us/library中所述/e4213hs1(VS.71).aspx 如果expression和conditional-expression都是l值,则结果是l值。

So I ran into something interesting that I didn't realize about the ternary operator (at least in Visual C++ 98-2010). As pointed out in http://msdn.microsoft.com/en-us/library/e4213hs1(VS.71).aspx if both the expression and conditional-expression are l-values the result is an l-value.

当然通常在c / c ++中,你会写如下:

Of course normally in c/c++ you'd write something like:

int value =(x == 1)? 1:0;

,甚至从不关心r值/ l值递归,在这种情况下,1和0都不是

and never even care about the r-value/l-value involvment, and in this case neither 1 nor 0 are convertible to l-values.

int value可以转换为l值。

However, take something like:

=(x == 1)? y:z;

int value = (x == 1) ? y : z;

y和z都是l值,而且更准确地说,其中之一是三元运算符(不是它的存储值),这不一定是明显的(至少我从来没有想过它在任何长度)。

both y and z are l-values and they, or more precisely, one of them is the actual result of the ternary operator (not its stored value) which isn't necessarily obvious (at least I had never thought about it at any length).

但是,导致的是能够编写以下

But, what that leads to is the ability to write the following

x = 1?y:z)= 99;

如果x == 1,则将99指定给y, != 1

Which assigns 99 to y if x == 1 or 99 to z if x != 1

我从来没有见过在任何地方和在我阅读过的关于使用(或者通常是否使用)三元运算符。

I've never seen that described anywhere and in all the discussions I've read about the use (or, usually, whether to use) the ternary operator.

当然,只有在表达式和条件表达式都是l值的情况下,它才有效。

Of course it only works if both the expression and conditional-expression are l-values something like

(x == 1?0:z)= 99;

这只适用于括号

<$> c $ c> x == 1? y:z = 99;

完全不同,只有当(x!= 1)两边都是l值,所以有什么东西像(x == 1?y:z = 99)= 100 do的严重漏洞根据x == 1的真值将100分配给y或z,如果x == 1为假,则对z = 99分配进行分割)

is something entirely different which assigns 99 to z only if (x != 1) and the beautiful part is that both sides are still l-values so there is the serious rat-hole of what things like (x == 1 ? y : z = 99) = 100 do (it assigns 100 to y or z depending on the truth of x == 1, stomping on the z = 99 assignment if x==1 is false)

我对我的问题:

A)这是实际的c ++标准的一部分(看起来像),而不只是一个微软的东西 - 我看过但已经失败,到目前为止,找到这个信息。

A) Is this part of the actual c++ standard (which seems like it would be) and not just a Microsoft thing -- I've looked but have failed, so far, to find this info.

B)如果这是广泛实现,我一直生活在岩石下?我从来没有见过它在任何我可以记得的代码中使用,从来没有看到它提到当三元运算符被讨论。

B) If this is widely realized and I've been living under a rock? I've never seen it used in any code that I can recall, and never seen it mentioned when the ternary operator is discussed.

C)我需要出去更多?

C) Do I need to get out more often?

推荐答案

A)是的,这是标准的一部分。

A) Yes, this is part of the standard.

B)它没有被广泛实现,虽然它可能在这里。有一个原因,它被投票为C ++的#1隐藏功能: http://stackoverflow.com/ question / 75538 / hidden-features-of-c-closed

B) It's not widely realized, though it may be here on SO. There's a reason it was voted the #1 hidden feature of C++: http://stackoverflow.com/questions/75538/hidden-features-of-c-closed.

C)没有评论。 :)

C) No comment. :)

我个人认为,建议您不要使用此功能。这比使用 if / else 语句更不直观,显然不是每个人都知道。

Personally, I recommend steering clear of using this feature. It is a lot less intuitive than using if/else statements, and clearly not everyone knows about it.

违反我自己的警告,我实际上尝试在个人项目中使用这一次,我被错过括号并浪费30分钟试图找到错误而被烧毁。

Going against my own warning, I actually tried using this once on a personal project, and I got burned by missing the parentheses and wasting 30 minutes trying to find the error.

这篇关于c ++三元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆