使用void表达式的条件运算符 [英] Conditional operator using void expression

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

问题描述

我在Linux内核的MTD驱动程序中运行了多个函数,这些函数让我感到有点尴尬。函数具有一般形式:


extern int bar(size_t len,size_t * retlen,unsigned char * buf);


int foo (size_t len,unsigned char * buf){

size_t retlen;

int ret;


ret = bar(len, & retlen,buf);

返回ret? :retlen; / *< - void表达式和UB? * /

}


这是foo()的回复声明,我正在征求意见。 Am

我更正了条件运算符的第二个操作数是一个void

表达式,如果是这样的话,那就不会通过
$来调用未定义的行为b $ b试图使用void表达式的值?我有时候很难解释这个标准并希望我不会忽视这些东西,但是我找不到可以祝福这个陈述的经文。

如果允许,foo()会返回非零值的值 -

零点?


谢谢,


-

Dan Henry

解决方案

Dan Henry写道:
< blockquote class =post_quotes>
我已经在Linux内核的MTD驱动程序中运行了多个函数,这些函数让我感到有点尴尬。函数具有一般形式:


extern int bar(size_t len,size_t * retlen,unsigned char * buf);


int foo (size_t len,unsigned char * buf){

size_t retlen;

int ret;


ret = bar(len, & retlen,buf);

返回ret? :retlen; / *< - void表达式和UB? * /

}


这是foo()的回复声明,我正在征求意见。 Am

我更正了条件运算符的第二个操作数是一个void

表达式,如果是这样的话,那就不会通过
$来调用未定义的行为b $ b试图使用void表达式的值?我有时候很难解释这个标准并希望我不会忽视这些东西,但是我找不到可以祝福这个陈述的经文。

如果允许,foo()返回非零值的值是什么 -

零?



我认为这是一个语法错误,但是gcc在

默认模式下没有投诉地接受它。它以严格模式发出警告:


gcc -Wall -pedantic -ansi xc

xc:函数`foo'':

xc:11:警告:ISO C禁止省略中间名?:表达式


我猜Linux内核黑客不使用严格模式:)


-

Ian Collins。


Dan Henry< us **** @ danlhenry.comwrites:


我在Linux内核的MTD驱动程序中运行了多个函数,这些函数让我感到有点尴尬。函数具有一般形式:


extern int bar(size_t len,size_t * retlen,unsigned char * buf);


int foo (size_t len,unsigned char * buf){

size_t retlen;

int ret;


ret = bar(len, & retlen,buf);

返回ret? :retlen; / *< - void表达式和UB? * /

}


这是foo()的回复声明,我正在征求意见。 Am

我更正了条件运算符的第二个操作数是一个void

表达式,如果是这样的话,那就不会通过
$来调用未定义的行为b $ b试图使用void表达式的值?



[...]


不,这不是虚空表达。 void表达式将是void类型的

表达式,例如(void)42。

''''和'':''之间出现的是一个空令牌序列,并不是

all的表达式。就标准C而言,它只是一个语法错误。


作为扩展,gcc允许条件

运算符的中间操作数被省略;有关详细信息,请参阅gcc文档。这个

是一个允许的扩展,因为它不会影响

任何严格符合程序的行为。


gcc与-pedantic相关选项正确地发出警告。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http:// users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长


2月26日,7日:34 pm,Dan Henry< use ... @ danlhenry.comwrote:


我在Linux内核的MTD驱动程序中遇到了

我有点挠头。函数具有一般形式:


extern int bar(size_t len,size_t * retlen,unsigned char * buf);


int foo (size_t len,unsigned char * buf){

size_t retlen;

int ret;


ret = bar(len, & retlen,buf);

返回ret? :retlen; / *< - void表达式和UB? * /


}


这是foo()的回复声明,我正在征求意见。 Am

我更正了条件运算符的第二个操作数是一个void

表达式,如果是这样的话,那就不会通过
$来调用未定义的行为b $ b试图使用void表达式的值?



这不是一个空表达式,而是缺少一个表达式。它不是有效的标准C,而是一个gcc扩展名,其中:

x? :y;

被视为:

x? x:y;


这是一个有用的扩展,但它不是标准的。


Robert Gamble


I have run across functions in the Linux kernel''s MTD driver that have
me scratching my head a bit. The functions have the general form:

extern int bar(size_t len, size_t *retlen, unsigned char *buf);

int foo(size_t len, unsigned char *buf) {
size_t retlen;
int ret;

ret = bar(len, &retlen, buf);
return ret ? : retlen; /* <-- void expression and UB? */
}

It is foo()''s return statement that I am soliciting comments about. Am
I correct that the conditional operator''s second operand is a void
expression and if so, doesn''t that invoke undefined behavior by
attempting to use the value of a void expression? I sometimes
struggle interpreting the standard and hope I haven''t overlooked
something, but I can''t find the verse that might bless the statement.
If it is allowed, what value would foo() return for nonzero ret --
zero?

Thanks,

--
Dan Henry

解决方案

Dan Henry wrote:

I have run across functions in the Linux kernel''s MTD driver that have
me scratching my head a bit. The functions have the general form:

extern int bar(size_t len, size_t *retlen, unsigned char *buf);

int foo(size_t len, unsigned char *buf) {
size_t retlen;
int ret;

ret = bar(len, &retlen, buf);
return ret ? : retlen; /* <-- void expression and UB? */
}

It is foo()''s return statement that I am soliciting comments about. Am
I correct that the conditional operator''s second operand is a void
expression and if so, doesn''t that invoke undefined behavior by
attempting to use the value of a void expression? I sometimes
struggle interpreting the standard and hope I haven''t overlooked
something, but I can''t find the verse that might bless the statement.
If it is allowed, what value would foo() return for nonzero ret --
zero?

I think it''s a syntax error, but gcc accepts it without complaint in
default mode. It does warn in strict mode:

gcc -Wall -pedantic -ansi x.c
x.c: In function `foo'':
x.c:11: warning: ISO C forbids omitting the middle term of a ?: expression

I guess Linux kernel hackers don''t use strict mode :)

--
Ian Collins.


Dan Henry <us****@danlhenry.comwrites:

I have run across functions in the Linux kernel''s MTD driver that have
me scratching my head a bit. The functions have the general form:

extern int bar(size_t len, size_t *retlen, unsigned char *buf);

int foo(size_t len, unsigned char *buf) {
size_t retlen;
int ret;

ret = bar(len, &retlen, buf);
return ret ? : retlen; /* <-- void expression and UB? */
}

It is foo()''s return statement that I am soliciting comments about. Am
I correct that the conditional operator''s second operand is a void
expression and if so, doesn''t that invoke undefined behavior by
attempting to use the value of a void expression?

[...]

No, it''s not a void expression. A void expression would be an
expression of type void, such as (void)42. What appears between the
''?'' and '':'' is an empty token sequence, and is not an expression at
all. As far as standard C is concerned, it''s simply a syntax error.

As an extension, gcc allows the middle operand of the conditional
operator to be omitted; see the gcc documentation for details. This
is a permissible extension, since it doesn''t affect the behavior of
any strictly conforming program.

gcc with the "-pedantic" option correctly issues a warning for this.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


On Feb 26, 7:34 pm, Dan Henry <use...@danlhenry.comwrote:

I have run across functions in the Linux kernel''s MTD driver that have
me scratching my head a bit. The functions have the general form:

extern int bar(size_t len, size_t *retlen, unsigned char *buf);

int foo(size_t len, unsigned char *buf) {
size_t retlen;
int ret;

ret = bar(len, &retlen, buf);
return ret ? : retlen; /* <-- void expression and UB? */

}

It is foo()''s return statement that I am soliciting comments about. Am
I correct that the conditional operator''s second operand is a void
expression and if so, doesn''t that invoke undefined behavior by
attempting to use the value of a void expression?

It''s not a void expression, it''s the lack of an expression. It isn''t
valid Standard C but is a gcc extension in which:
x ? : y;
is treated as:
x ? x : y;

It''s a useful extension but it isn''t Standard.

Robert Gamble


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

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