逗号操作员问题 [英] Comma Operator Question

查看:78
本文介绍了逗号操作员问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承的一些代码包含如下宏:


#define setState(state,newstate)\

(state> =新州)? \\ b
(fprintf(stderr,Illegal state\ n),TRUE):\

(state = newstate,FALSE)


这个宏被调用如下:


setState(state,ST_Used);


我明白了这个宏除非新

状态大于旧状态,否则会输出错误消息。我不理解TRUE和

FALSE位。

看起来逗号运算符导致宏评估为TRUE

或假。我是对的吗?

宏评估为TRUE或FALSE的结果是什么?

很难知道原始程序员在想什么,但可以

他一直打算将宏用作条件吗?


if(setState(state,ST_Used))

do什么...


关于为何使用逗号运算符的任何想法都将是

赞赏。


谢谢,

Shawn

Some code I have inherited contains a macro like the following:

#define setState(state, newstate) \
(state >= newstate) ? \
(fprintf(stderr, "Illegal state\n"), TRUE) : \
(state = newstate, FALSE)

This macro is called like this:

setState(state, ST_Used);

I understand that the macro prints an error message unless the new
state is greater than the old state. I don''t understand the TRUE and
FALSE bits.
It looks like the comma operator causes the macro to evaluate to TRUE
or FALSE. Am I right?
What is the result of the macro evaluating to TRUE or FALSE?
It''s hard to know what the original programmer was thinking, but could
he have been intending the macro to be used as a condition?

if (setState(state, ST_Used))
do something...

Any thoughts about why the comma operator is being used will be
appreciated.

Thanks,
Shawn

推荐答案

2004年6月29日星期二11:42:36 -0700,Shawn Odekirk写道:
On Tue, 29 Jun 2004 11:42:36 -0700, Shawn Odekirk wrote:
我继承的一些代码包含如下宏:

#define setState(state,newstate)\
(state> = newstate)? \\ /
(fprintf(stderr,Illegal state\ n),TRUE):\
(state = newstate,FALSE)

此宏被调用为这个:

setState(state,ST_Used);

我知道宏会打印一条错误信息,除非新的状态大于旧状态。我不理解TRUE和
FALSE位。
看起来逗号运算符导致宏评估为TRUE
或FALSE。我对吗?
No.

逗号将两个参数分隔为一个名为state和newstate的宏

第二行是一个比较,如果state为

大于或等于newstate否则它是假的

宏是建立在一个叫做tri-graph的旧设备上的,它就像

这个

< some expresion> ? statement1:statement2


expresion返回一个值(true或false),如果为true

语句1被执行。如果是false语句2被执行。


所有语句都返回一个值,无论你是否使用它。在一个逗号

分隔的语句列表中,语句的结果是

的最后一个语句的结果,所以原来的程序员''return''要么是真的
或false最有可能用于条件测试,如下所示。


宏评估为TRUE或FALSE的结果是什么?
它'很难知道原来的程序员在想什么,但是他可能已经打算将宏用作条件吗?

if(setState(state,ST_Used))
任何有关逗号运算符使用原因的想法都将受到赞赏。


这里使用逗号来确保返回值是正确的

类型。由逗号分隔的任何语句都按顺序执行,但

返回值是最后一个语句的值以及该类型的值。


宏将使用任何数字类型,但由于

逗号返回一个布尔非常有用,如果你想做 -

/ * snipity snip * /

布尔标志;


..

..

..

flag = setState(var1,var5);

谢谢,
Shawn
Some code I have inherited contains a macro like the following:

#define setState(state, newstate) \
(state >= newstate) ? \
(fprintf(stderr, "Illegal state\n"), TRUE) : \
(state = newstate, FALSE)

This macro is called like this:

setState(state, ST_Used);

I understand that the macro prints an error message unless the new
state is greater than the old state. I don''t understand the TRUE and
FALSE bits.
It looks like the comma operator causes the macro to evaluate to TRUE
or FALSE. Am I right? No.
The comma separates two arguments to a macro called state and newstate
The second line is a comparison which evaluates to true if state is
greater than or equal to newstate otherwise it is false
The macro is built on a old device called a tri-graph which works like
this
<some expresion> ? statement1 : statement2

The expresion returns a value (true or false) and if it is true
statement 1 is executed. If it is false statement 2 is executed.

All statements return a value whether or not you use it. in a comma
seperated list of statements the result of the statement is the result of
the last statement so the original programmer is ''returning'' either true
or false most probably to use in conditional tests as you suggest below.

What is the result of the macro evaluating to TRUE or FALSE?
It''s hard to know what the original programmer was thinking, but could
he have been intending the macro to be used as a condition?

if (setState(state, ST_Used))
do something...

Any thoughts about why the comma operator is being used will be
appreciated.
The comma is used here to make sure the return value is of the correct
type. Any statements seperated by a comma are executed in order but the
return value is the value of the last statement and also of that type.

The macro would work with any numerical type but because of the
comma returns a boolean very useful if you want to do ---
/* snipity snip*/
boolean flag;

..
..
..
flag=setState(var1, var5);

Thanks,
Shawn




PS假是'0'',真=非零结果


OK

Trevor



P.S. False is ''0'', true= non zero result

OK
Trevor

sh***********@fkilogistex.com ( Shawn Odekirk)在留言新闻中写道:< d2 ************************** @ posting.google。 com> ...
sh***********@fkilogistex.com (Shawn Odekirk) wrote in message news:<d2**************************@posting.google. com>...
我继承的一些代码包含如下宏:

#define setState(state,newstate)\
(state> ; = newstate)? \\ /
(fprintf(stderr,Illegal state\ n),TRUE):\
(state = newstate,FALSE)

此宏被调用为这个:

setState(state,ST_Used);

我知道宏会打印一条错误信息,除非新的状态大于旧状态。我不理解TRUE和
FALSE位。
看起来逗号运算符导致宏评估为TRUE
或FALSE。我是对的吗?
宏评估为TRUE或FALSE的结果是什么?
很难知道原始程序员在想什么,但是他可能已经打算宏用作条件?

if(setState(state,ST_Used))
做某事...

关于为什么逗号运算符的任何想法将被赞赏。
Some code I have inherited contains a macro like the following:

#define setState(state, newstate) \
(state >= newstate) ? \
(fprintf(stderr, "Illegal state\n"), TRUE) : \
(state = newstate, FALSE)

This macro is called like this:

setState(state, ST_Used);

I understand that the macro prints an error message unless the new
state is greater than the old state. I don''t understand the TRUE and
FALSE bits.
It looks like the comma operator causes the macro to evaluate to TRUE
or FALSE. Am I right?
What is the result of the macro evaluating to TRUE or FALSE?
It''s hard to know what the original programmer was thinking, but could
he have been intending the macro to be used as a condition?

if (setState(state, ST_Used))
do something...

Any thoughts about why the comma operator is being used will be
appreciated.




1.可能是因为它可用于if或while声明


2. TRUE,FALSE和状态类型在代码段中未定义,

如果类型为state,则为b。不是int,或者可以自由转换为int,

然后一些编译器可能会生成错误或警告(免责声明,我

没有标准的副本)。我相信

?:运算符的两个操作数必须具有相同的类型(但我可能是错的)。



1. Probably so that it can be used in an if or while statement

2. TRUE, FALSE, and the type of state are undefined in the snippet,
if the type of "state" is not an int, or freely convertible to int,
then some compilers may generate an error or warning (disclaimer, I
don''t have a copy of the standard). I believe that both operands of a
?: operator must have the same type (but I may be wrong).


2004年6月29日11:42:36 -0700, sh *********** @ fkilogistex.com (Shawn

Odekirk)写道:
On 29 Jun 2004 11:42:36 -0700, sh***********@fkilogistex.com (Shawn
Odekirk) wrote:
我继承的一些代码包含如下宏:

# define stateState(state,newstate)\
(state> = newstate)? \\ /
(fprintf(stderr,Illegal state\ n),TRUE):\
(state = newstate,FALSE)

此宏被调用为这个:

setState(state,ST_Used);

我知道宏会打印一条错误信息,除非新的


否,宏没有打印任何东西。

状态大于旧状态。我不理解TRUE和
FALSE位。
看起来逗号运算符导致宏评估为TRUE
或FALSE。我对吗?


宏是预处理器的输入。他们评估

的唯一内容是替换文本然后传递给编译器。当你的例子中使用了

时,宏评估为

(状态> = ST_Used)?

(fprintf(...) ,TRUE):

(state = ST_Used,FALSE);


TRUE和FALSE也可能是宏,应该在
$之前定义b $ b setState。


现在,如果这段代码被执行,那么两件事之一就会发生:b / b $

字符串将被发送到stderr,表达式将评估

为TRUE(我希望为1)。


值在状态将被更改,表达式将

评估为FALSE(我希望为0)。

宏评估为TRUE或FALSE的结果是什么?
很难知道原来的程序员在想什么,但是他可能会将宏用作条件吗?

if(setState(州,ST_Used))
做点什么...


一个合理的假设。不一定是真的,但合理。如果你有理由相信原作者有能力,那么即使很可能也可能是b $ b。但后来我们不得不问他为什么不留下任何

文件,所以你不必猜测。你看到任何代码

看起来像这样吗?他多久调用一次宏?

任何关于为何使用逗号运算符的想法都将受到赞赏。
Some code I have inherited contains a macro like the following:

#define setState(state, newstate) \
(state >= newstate) ? \
(fprintf(stderr, "Illegal state\n"), TRUE) : \
(state = newstate, FALSE)

This macro is called like this:

setState(state, ST_Used);

I understand that the macro prints an error message unless the new
No, the macro doesn''t print anything.
state is greater than the old state. I don''t understand the TRUE and
FALSE bits.
It looks like the comma operator causes the macro to evaluate to TRUE
or FALSE. Am I right?
Macros are inputs to the preprocessor. The only thing they evaluate
to is the substituted text which is then passed to the compiler. When
used as in your example, the macro evaluates to
(state >= ST_Used) ?
(fprintf(...), TRUE) :
(state = ST_Used, FALSE);

TRUE and FALSE are probably also macros and should be defined prior to
setState.

Now, if this code is ever executed, then one of two things will
happen:

The string will be sent to stderr and the expression will evaluate
to TRUE (which I would expect to be 1).

The value in state will be changed and the expression will
evaluate to FALSE (which I would expect to be 0).
What is the result of the macro evaluating to TRUE or FALSE?
It''s hard to know what the original programmer was thinking, but could
he have been intending the macro to be used as a condition?

if (setState(state, ST_Used))
do something...
A reasonable assumption. Not necessarily true but reasonable. If you
have reason to believe the original author was competent, it might
even be probable. But then we have to ask why he didn''t leave any
documentation so you wouldn''t have to guess. Do you see any code that
looks like this? How often does he invoke the macro?

Any thoughts about why the comma operator is being used will be
appreciated.




因为宏可以很容易地评估if-then-else,

我猜*是他认为他是聪明的。如果你真的需要

来知道,你可以在其中一个ESP新闻组中询问,但这里只是猜测是



< <删除电子邮件的del>>



Since the macro could have just as easily evaluate to an if-then-else,
my *guess* is that he thought he was being clever. If you really need
to know, you could ask in one of the ESP newsgroups but here it is
only a guess.
<<Remove the del for email>>


这篇关于逗号操作员问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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