没有中间表达式的三元运算符 [英] Ternary operator without the middle expression

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

问题描述

我最近意识到你可以在没有中间(?:?)的GCC和clang中使用三元运算符: works),它将把第一个表达式插入中间:

I realized recently that you can use the ternary operator in GCC and clang without a middle (?: or ? : works) and it will insert the first expression into the middle:

// outputs 2
cout << (2 ?: 4);
// outputs 3
cout << (0 ?  : 3);

标准中的哪里?

推荐答案

它不是在标准中

It isn't in the standard at all.

您观察到的是一个GCC扩展: https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html

What you are observing is a GCC extension: https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html

如果省略它,它的值取自第一个操作数在上下文转换为 bool 之前。

扩展值在于不重复副作用并减少源代码大小。

If you omit it, its value is taken from the first operand prior to contextual conversion to bool.
The extensions value lies in not repeating side-effects and reducing the source-codes size.

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

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