C逗号操作符 [英] C comma operator

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

问题描述

为什么在逗号运算符(例如下面的示例)中指定的表达式不被视为常量表达式?

Why is the expression specified inside a comma operator (such as the example below) not considered a constant expression?

例如,

int a = (10,20) ;

当在全局范围内给出时会产生一个错误initializer不是常量,尽管这两个表达式分离一个逗号运算符是常量(常量表达式)。为什么整个表达式不被视为一个常量表达式?为了澄清,我已经阅读了 ','操作符在C ? 使用C逗号操作符

when given in global scope yields an error "initializer is not a constant", though both the expressions separated by a comma operator are constants (constant expressions). Why is the entire expression is not treated as a constant expression? For clarification I have read What does the ‘,’ operator do in C? and Uses of C comma operator. They have not dealt this aspect of comma operator.

推荐答案

部分 6.6 / 3 ,ISO C99标准的常数表达式是您需要的部分。它指出:

Section 6.6/3, "Constant expressions", of the ISO C99 standard is the section you need. It states:


常量表达式不得包含赋值,递增,递减,函数调用,
或逗号运算符,它们包含在不是
评估的子表达式中。

Constant expressions shall not contain assignment, increment, decrement, function-call, or comma operators, except when they are contained within a subexpression that is not evaluated.

在ISO的C99理由文档中,有一小段代码:

In the C99 rationale document from ISO, there's this little snippet:


整数常量表达式必须只涉及翻译时可知的数字,而没有副作用的操作符。

An integer constant expression must involve only numbers knowable at translation time, and operators with no side effects.

而且,如果您不依赖副作用,因为使用逗号操作符没有任何意义,所以在一个常数表达式。

And, since there's no point in using the comma operator at all if you're not relying on side effects, it's useless in a constant expression.

这样,我的意思是两个代码段之间绝对没有区别:

By that, I mean there's absolutely no difference between the two code segments:

while (10, 1) { ... }
while     (1) { ... }

,因为 10 实际上并不是。实际上,

since the 10 doesn't actually do anything. In fact,

10;

是一个非常有效的,虽然不是非常有用的C语句,大多数人不了解,直到他们更好地了解这种语言。

is a perfectly valid, though not very useful, C statement, something most people don't comprehend until they get to know the language better.

但是,这两个语句之间的区别是:

However, there is a difference between these two statements:

while (  10, 1) { ... }
while (x=10, 1) { ... }

后一种使用逗号运算符的副作用是设置变量 x 10

There's a side effect in the latter use of the comma operator which is to set the variable x to 10.

至于为什么他们不喜欢常量表达式中的副作用,整个常量表达式的一点是,它们可以在编译时进行评估,而不需要执行环境 - ISO对转换(编译时)和执行(运行时)环境进行区分。

As to why they don't like side effects in constant expressions, the whole point of constant expressions is that they can be evaluated at compile-time without requiring an execution environment - ISO makes a distinction between translation (compile-time) and execution (run-time) environments.

关于为什么ISO决定不要求编译器提供执行环境信息(除了包含在头文件中的东西(例如 limits.h )可以在理论文档稍后找到:

The clue as to why ISO decided against requiring compilers to provide execution environment information (other than stuff contained in header files such as limits.h) can be found a little later in the rationale document:


然而,虽然实现是肯定允许在翻译和执行环境中产生完全相同的结果,要求这被认为是许多交叉编译器的难以承受的负担。

However, while implementations are certainly permitted to produce exactly the same result in translation and execution environments, requiring this was deemed to be an intolerable burden on many cross-compilers.



<换句话说,ISO不希望交叉编译器的制造商承担为每一个可能的目标携带执行环境。

In other words, ISO didn't want the manufacturers of cross-compilers to be burdened with carrying an execution environment for every possible target.

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

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