为什么要使用三元运算符而不为“true”赋值?条件 (x = x ?: 1)
在 Android 开源 qemu 代码中,我遇到了这行代码:
machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
这只是一种令人困惑的说法吗:
if (machine->max_cpus) {
; //do nothing
} else {
machine->max_cpus = 1;
}
如果是这样,不是更清楚吗:
if (machine->max_cpus == 0) machine->max_cpus = 1;
有趣的是,这可以与 gcc 一起编译并正常工作,但不能在 http://www.comeaucomputing.com/tryitout/ 上编译。
In the Android open-source qemu code I ran across this line of code:
machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
Is this just a confusing way of saying:
if (machine->max_cpus) {
; //do nothing
} else {
machine->max_cpus = 1;
}
If so, wouldn't it be clearer as:
if (machine->max_cpus == 0) machine->max_cpus = 1;
Interestingly, this compiles and works fine with gcc, but doesn't compile on http://www.comeaucomputing.com/tryitout/ .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是 GNU 中允许的模糊扩展至 C
正如您可能猜到的那样,出于可读性和可移植性的原因,建议避免这种情况。老实说,我很惊讶看到这样一个语法不兼容的 C 扩展。
This is permitted in GNU as an obscure extension to C
As you can probably guess, avoiding this is recommended for readability and portability reasons. I'm honestly surprised to see such a grammar-incompatible extension to C.
这是一个 GCC 扩展,意思是“如果条件为 true,使用它,否则使用其他值”,so
是简写,
尽管如果条件有副作用,则只会运行一次
This is a GCC extension that means "if the condition is true, use it, else use this other value", so
is shorthand for
although if the conditional has side-effects, it will only be run once
使用 gcc 的 -pedantic 标志,它确实说
Using gcc's -pedantic flag, it does say
这是一个 GCC 扩展,而且它变得更有趣当病情有副作用时很有用。
在这种情况下,是的,我个人认为它比其他任何事情都更晦涩难懂。
It's a GCC extension, and it gets more interesting and useful when the condition has side effects.
In this case, yes, I for one would agree it's obscure more than anything else.
K&R BNF 显示“?”之间需要一个表达式。和 ”:”。我认为 gcc 不应该在没有诊断的情况下编译它。
The K&R BNF shows an expression is required between "?" and ":". I don't think gcc should be compiling that without a diagnostic.
还有另一个有用的例子——在调用可能返回 nil 的函数或方法时消除中间变量,我们希望避免调用两次。例如(Objective-C),假设我们想要将一个文件解压到一个数组中(如果存在),否则返回一个空数组。
替代方案不太简洁。
或者更丑陋的多个返回等。
所以它是有用的语法糖,我发现相当可读。缺点是
惯例,但大多数现代语言不允许这样做,这使情况变得复杂
任何移植工作。
正如其他人所说,它也是一个非标准扩展,所以它应该
如果完全考虑可移植性,则应避免。
There's another useful case for this -- the elimination of intermediate variables when calling a function or method that might return nil, that we wish to avoid calling twice. For example (Objective-C), suppose we want to unpack a file into an array if it exists, otherwise return an empty array.
The alternatives are less concise.
Or uglier with multiple returns etc.
So it's useful syntactic sugar that I find fairly readable. The downsides are
Implicit conversion of a pointer to a bool. This is a long-standing C
convention, but most modern languages disallow it, complicating
any porting efforts.
As others have said it's also a non-standard extension, so it should
be avoided if portability is a consideration at all.
我觉得其他答案没有回答标题中的问题,并且还考虑了标签
c
。因此我添加另一个答案。我使用这种语法来防止我的代码因 if 语句而变得丑陋。
您可以在该行的开头看到实际的函数调用。将其与下面的版本进行比较,其中前导的
if
阻碍了函数调用的直接查看。甚至更难阅读:
I feel the other answers do not answer the question in the title and also taking the tag
c
into account. Therefore I add another answer.I use this syntax to prevent my code from getting ugly through if-statements.
You can see the actual function call right in the beginning of the line. Compared it to the versions below, where the leading
if
obstructs the direct view of the function call.or even harder to read: