离奇的使用在Linux中条件运算符 [英] Bizarre use of conditional operator in Linux

查看:185
本文介绍了离奇的使用在Linux中条件运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在3.0.4 Linux内核,毫米/ filemap.c有这一行code的:

In the 3.0.4 Linux kernel, mm/filemap.c has this line of code:

retval = retval ?: desc.error;

我试过编译类似最小测试用例用gcc -Wall并没有得到任何警告;的行为似乎相同的:

I've tried compiling a similar minimal test case with gcc -Wall and don't get any warnings; the behavior seems identical to:

retval = retval ? retval : desc.error;

综观C99标准,我想不出有什么正式介绍了这种行为。这是为什么好?

Looking at the C99 standard, I can't figure out what formally describes this behavior. Why is this OK?

推荐答案

随着其他几个人都表示,这是一个GCC扩展,没有任何标准的一部分。如果您使用 -pedantic 开关,你会得到它的警告。

As several others have said, this is a GCC extension, not part of any standard. You'll get a warning for it if you use the -pedantic switch.

的的这个扩展是不是在这种情况下确实可见,但想象一下,如果相反,它是

The point of this extension is not really visible in this case, but imagine if instead it was

retval = foo() ?: desc.error;

通过扩展,富()只调用一次。没有它,你必须引入一个临时变量,以避免调用富()两次。

With the extension, foo() is called only once. Without it, you have to introduce a temporary variable to avoid calling foo() twice.

这篇关于离奇的使用在Linux中条件运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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