C 逗号运算符 [英] C comma operator

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

问题描述

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

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

例如

int a = (10,20) ;

在全局范围内给出时会产生错误初始化程序不是常量",尽管由逗号运算符分隔的两个表达式都是常量(常量表达式).为什么整个表达式不被视为常量表达式?为了澄清起见,我已阅读 ',' 运算符在 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天全站免登陆