滥用条件运算符(fwd) [英] abuse of conditional operators (fwd)

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

问题描述



我的一个朋友问我一个我无法回答的C问题。可以

你们有权重吗?


----------转发的消息----------

日期:2004年11月21日星期日21:08:27 -0500

来自:Rick Kennell< ri ** @ kennell.org>

收件人: no*****@purdue.edu

主题:滥用条件运算符


几年前,当我编写编译器时,我感到非常困惑。

发现有些人希望这可以工作:


a? b:c = d;


即如果''a''非零,则将''d''分配给''b'',否则分配''d''到''c''。

从技术上讲,由于X3J11

特别声称条件运算符没有产生

左值。 GCC处理它和现代版本至少警告它这是'b $ b弃用行为。 TCC默默地扼杀了它。我不知道其他的

编译器。

所以昨天,我正在阅读LKML,遇到了一个打破了

代码的人 - 检查系统因为某些东西被编码为:


a = b? :c;


具体来说,代码是:


int tickadj = 500 / HZ? :1;


将tickadj设置为500 / HZ或者如果HZ大于500则设置为1.


实际上,这意味着


a = b? :c;


相当于


a = b? b:c;


原始的K& R和我的X3J11副本都没有说明这一点。

GCC文档中有关于它的说明

http://gcc.gnu.org/onlinedocs/gcc-3....l#Conditionals


但它对C99没有任何说法。假设它只是一个海湾合作委员会,我是否正确?


Rick


A friend of mine asked me a C question that I couldn''t answer. Could
you guys weigh in?

---------- Forwarded message ----------
Date: Sun, 21 Nov 2004 21:08:27 -0500
From: Rick Kennell <ri**@kennell.org>
To: no*****@purdue.edu
Subject: abuse of conditional operators

I was pretty bemused a few years ago when I was writing a compiler and
found out that some people expected this to work:

a ? b : c = d;

i.e. if ''a'' is non-zero, assign ''d'' to ''b'', otherwise assign ''d'' to ''c''.
Technically, there''s no guarantee of support for this since X3J11
specifically claims that the conditional operator does not yield an
lvalue. GCC handles it and modern versions at least warn that this it''s
deprecated behavior. TCC silently bungles it. I don''t know about other
compilers.
So yesterday, I was reading LKML and came across someone who broke a
code-checking system because something was coded as:

a = b ? : c;

Specifically, the code was:

int tickadj = 500/HZ ? : 1;

which sets tickadj either to 500/HZ or to 1 if HZ is larger than 500.

Effectively, this means that

a = b ? : c;

is equivalent to

a = b ? b : c;

Neither the original K&R nor my copy of X3J11 say nothing about this.
There is a note about it in the GCC documentation

http://gcc.gnu.org/onlinedocs/gcc-3....l#Conditionals

but it doesn''t say anything about C99. Am I correct in assuming it''s
just a GCCism?

Rick

推荐答案

Dan Noland< no ****** @ cs.purdue.edu>写道:
Dan Noland <no******@cs.purdue.edu> wrote:
几年前,当我编写编译器时,我感到非常困惑,并且发现有些人希望这可以工作:
一个? b:c = d;

即如果''a''非零,则将''d''分配给''b'',否则将''d''分配给''c' ''。
从技术上讲,由于X3J11特别声称条件运算符没有产生值,因此无法保证对此有所支持。


< http://www.eskimo.com/~scs/C-faq/q3.16.html>

昨天,我是阅读LKML并遇到了一个打破了代码检查系统的人,因为有些东西被编码为:

a = b? :c;
I was pretty bemused a few years ago when I was writing a compiler and
found out that some people expected this to work:

a ? b : c = d;

i.e. if ''a'' is non-zero, assign ''d'' to ''b'', otherwise assign ''d'' to ''c''.
Technically, there''s no guarantee of support for this since X3J11
specifically claims that the conditional operator does not yield an
lvalue.
<http://www.eskimo.com/~scs/C-faq/q3.16.html>
So yesterday, I was reading LKML and came across someone who broke a
code-checking system because something was coded as:

a = b ? : c;




这是一个语法错误。可能是Ganuck允许的; ISO没有,既不是b $ b $ C $也不是C99。


Richard



That''s a syntax error. Possibly Ganuck allows it; ISO does not, neither
C89 nor C99.

Richard


Richard Bos写道:
Richard Bos wrote:
Dan Noland< no ****** @ cs.purdue.edu>写道:

Dan Noland <no******@cs.purdue.edu> wrote:

几年前,当我编写编译器时,我感到非常困惑,并且发现有些人希望这可以工作:

一个? b:c = d;

即如果a非零,则将d分配给b,否则将d分配给c。
从技术上讲,不能保证自X3J11以来对此的支持
明确声称条件运算符不会产生值左右。
I was pretty bemused a few years ago when I was writing a compiler and
found out that some people expected this to work:

a ? b : c = d;

i.e. if ''a'' is non-zero, assign ''d'' to ''b'', otherwise assign ''d'' to ''c''.
Technically, there''s no guarantee of support for this since X3J11
specifically claims that the conditional operator does not yield an
lvalue.



< http://www.eskimo.com/ ~scs / C-faq / q3.16.html>


<http://www.eskimo.com/~scs/C-faq/q3.16.html>

昨天,我正在阅读LKML并遇到一个打破了代码检查系统的人,因为有些东西是编码为:

a = b? :c;
So yesterday, I was reading LKML and came across someone who broke a
code-checking system because something was coded as:

a = b ? : c;



这是一个语法错误。可能是Ganuck允许的; ISO没有,既不是C89也不是C99。


That''s a syntax error. Possibly Ganuck allows it; ISO does not, neither
C89 nor C99.



Ganuck(LOL :-)允许它。来自
http:/ /gcc.gnu.org/onlinedocs/gcc-3....l#Conditionals

5.8省略操作数的条件


可以省略条件表达式中的中间操作数。然后如果

第一个操作数是非零的,它的值是条件

表达式的值。


因此,表达式


x? :y

如果非零,
的值为x;否则,y的值。


这个例子完全相当于


x? x:y


在这个简单的例子中,省略中间操作数的能力不是特别有用的。当它变得有用时是第一个操作数

,或者可能(如果它是一个宏参数),包含副作用。然后

重复中间的操作数将执行两次副作用。

省略中间操作数使用已计算的值而不使用

重新计算它的效果。

[结束语]


为什么他们选择扩展这样的语言超出了我的范围,因为它b / b
应该很容易使用标准C并且仍然可以避免任何重新计算。


Bj?rn


Dan Noland schrieb:
Dan Noland schrieb:
具体来说,代码是:

int tickadj = 500 / HZ? :1;

如果HZ大于500,则将tickadj设置为500 / HZ或设置为1。
Specifically, the code was:

int tickadj = 500/HZ ? : 1;

which sets tickadj either to 500/HZ or to 1 if HZ is larger than 500.




嗯,我宁愿猜测的目的是避免被零除!!


所以它应该读作:500 /(HZ?:1)


Sascha



Huh, I would rather guess the purpose is to avoid a division by zero!?

So it should be read as: 500/(HZ ? : 1)

Sascha


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

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