c++三元运算符

发布于 2024-09-10 05:25:49 字数 1264 浏览 7 评论 0原文

因此,我遇到了一些关于三元运算符的有趣的事情,但我没有意识到(至少在 Visual C++ 98-2010 中)。正如 http://msdn.microsoft.com/ 中指出的en-us/library/e4213hs1(VS.71).aspx 如果表达式和条件表达式均为左值,则结果为左值。

当然,通常在 c/c++ 中你会写这样的内容:

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

甚至从不关心右值/左值的参与,在这种情况下,1 和 0 都不能转换为左值。

然而,采取类似:

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

y 和 z 都是左值,它们,或者更准确地说,其中之一是三元运算符的实际结果(不是它的存储值),这不一定是显而易见的(至少我从来没有认真思考过这个问题)。

但是,这会导致能够编写以下

代码 (x == 1 ? y : z) = 99;

如果 x == 1,则将 99 分配给 y;如果 x !,则将 99 分配给 z = 1

我从未在任何地方以及我读过的有关使用(或者通常是否使用)三元运算符的所有讨论中看到过这种描述。

当然,只有当表达式和条件表达式都是左值时,例如

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

无法编译,因为 0 是右值,编译器高兴地指出。

只有包含括号

x == 1 才有效? y : z = 99;

是完全不同的东西,它仅在 (x != 1) 时才将 99 分配给 z,而美妙的部分是两边仍然是左值,因此存在严重的鼠洞像 (x == 1 ? y : z = 99) = 100 这样的事情(它根据 x == 1 的真实情况将 100 分配给 y 或 z,如果满足 z = 99 的赋值,则踩踏 z = 99 的赋值) x==1 是 false)

所以,这引出了我的问题:

A)这是否是实际的 C++ 标准的一部分(看起来应该是)而不仅仅是 Microsoft 的东西 - 我已经看过但失败了,到目前为止,找到这个信息。

B) 如果这被广泛认识到,而我却一直生活在岩石下呢?我从未见过它在我能记得的任何代码中使用过,也从未在讨论三元运算符时见过它被提及。

C) 我需要更频繁地出去吗?

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.

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

int value = (x == 1) ? 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.

However, take something like:

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

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;

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.

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

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

fails to compile because 0 is an r-value as happily pointed out by the compiler.

And this only works if you include the parenthesis

x == 1 ? y : 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)

So, this leads me to my questions:

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) 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) Do I need to get out more often?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笨死的猪 2024-09-17 05:25:49

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

B)它还没有被广泛意识到,尽管它可能在这里。它被评为 C++ 的 #1 隐藏功能是有原因的:C++ 的隐藏功能?

C) 没有评论。 :)

就我个人而言,我建议避免使用此功能。它比使用 if/else 语句要直观得多,而且显然不是每个人都知道它。

与我自己的警告相反,我实际上在个人项目中尝试使用过一次,但由于缺少括号并浪费了 30 分钟试图找到错误而被烧伤。

A) Yes, this is part of the standard.

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++: Hidden Features of C++?.

C) No comment. :)

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.

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.

笑看君怀她人 2024-09-17 05:25:49

答。是的。 §[expr.cond]/4:

如果第二个和第三个操作数是左值并且具有相同的类型,则结果就是该类型并且是左值...

(请注意,这在 C 中不是 true。在 C99 标准中明确编写,脚注 93,“条件表达式不会产生左值。”)

B. 我不认为它被广泛使用,因为用法相当模糊。比较常见的是

if (x == 1)
  y = 99;
else
  z = 99;

A. Yes. §[expr.cond]/4:

If the second and third operands are lvalues and have the same type, the result is of that type and is an lvalue ...

(Note that it is not true in C. Explicitly written in the C99 standard, footnote 93, "A conditional expression does not yield an lvalue.")

B. I don't think it's widely used as the usage is pretty obscure. It's more common to see

if (x == 1)
  y = 99;
else
  z = 99;
情释 2024-09-17 05:25:49

您从未见过它的使用,因为这种用法不如最常见的用法直观和可读。我也从未见过它在生产代码中以这种方式使用,我希望我永远不会看到它。

还记得赫伯·萨特和赫伯·萨特吗? Andrei Alexandrescu 的 C++ 编码标准,规则 6:“正确性、简单性和清晰度是第一位的。”

You have never seen it used because this usage is less intuitive and readable than the most common one. I have never seen it used this way in production code, either, and I hope I never see it.

Remember Herb Sutter & Andrei Alexandrescu's C++ Coding Standards, rule 6: "Correctness, simplicity, and clarity come first."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文