关于强制转换为整数常量表达式(在标准C中) [英] About cast in integer constant expression (in standard C)

查看:194
本文介绍了关于强制转换为整数常量表达式(在标准C中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在标准C(我的意思是C99或C11)中,我们有所谓的整数常量表达式,它们是操作数均为常量整数的常量表达式.还有其他限制,以避免在表达式中使用逗号运算符.

In standard C (I mean C99 or C11) we have the so-called integer constant expressions, which are constant expressions whose operands are all constant integers. There are other constraints, as to avoid comma operators in the expression.

但是,在某些特殊情况下,允许使用其他非整数对象(甚至非常数).
例如,如果将sizeof运算符应用于其大小在转换时已知的对象,则允许将其作为整数常量表达式的一部分(请注意,sizeof始终返回整数值) ).

However, other non-integer objects (even non-constant) are allowed in some special cases.
For example, if the sizeof operator is applied to an object whose size is known in translation time, this is allowed as part of an integer constant expression (note that sizeof always return an integer value).

此外,有时也允许将显式强制转换为整数类型.
标准C99建立以下规则:

Moreover, the explicit cast to an integer type is sometimes allowed too.
The standard C99 establish the following rule:

标准C99,第6.6节(第6节):

一个整数常量表达式)应具有整数类型,并且应 仅具有整数消耗量,枚举常数, 字符常量,sizeof表达式,其结果为整数 常量和作为常量的立即数的浮点常量 演员.

An integer constant expression) shall have integer type and shall only have operands that are integer costants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and floating constants that are the immediate operands of casts.

标准C99

我的问题是:作为强制类型转换的立即数的浮点常量"的确切含义是什么?

My question is: Which is the exact meaning of "floating constants that are the immediate operands of casts"?

浮点常量类似于3.14e + 3或0x2.11p-5.
也就是说,它不是float类型的一般常量表达式,而只是浮点文字.
然后,我了解到上面的定义只允许这样的内容:

A floating constant is something like 3.14e+3, or well 0x2.11p-5.
That is, is not a general constant expression of type float, but only a floating point literal.
Then, I understand that only something like this is allowed in the definition above:

 (int) 3.14

但不允许进行涉及浮点文字的操作.
排除以下情况:

but are not allowed operations involving floating literals.
This exclude cases as the following:

 (int) -3.14  /* The minus sign is not part of the constant: it is an operator */
 (int) (3.14) /* The parenthesis are an operator acting on the literal 3.14 */

最后一种情况不需要在转换时执行任何浮点算术运算,并且等效于不带括号的情况:(int) 3.14.
但是,它不是强制转换的立即操作数.

The last case does not need to perform any floating point arithmetical operations in translation time, and it is equivalent to have the same without parenthesis: (int) 3.14.
However, it is not the immediate operand of a cast.

因此,我们是否必须考虑(int) (3.14)是有效的整数常量表达式的一部分(根据定义)?

So, do we have to consider that (int) (3.14) is [part of] a valid integer constant expression or not (according to definition)?

另一方面,编译器GCC(带有选项:-std = c99 -pedantic-errors)告诉我(int) (3.14)是有效的整数常量表达式,例如在声明为以下:

On the other hand, the compiler GCC (with options: -std=c99 -pedantic-errors) gives me that (int) (3.14) is a valid integer constant expression, for example in a declaration as the following:

 #define BITW (int) (3.14)
 struct { unsigned bitfield: BITW } test;  // Translation success

(但是,通过执行#define BITW (int) (+3.14),它未能按预期翻译).

(However, by doing #define BITW (int) (+3.14) it fails to translate, as expected).

推荐答案

虽然措词选择不当可能意味着(int) (3.14)不符合整数常量表达式的要求,但我认为这只是措辞中的错误.有一个类似的问题,可以说是NULL规范的错误:它可以是任何空指针常量,其中空指针常量定义为:

While poor choice of wording may imply that (int) (3.14) does not qualify as an integer constant expression, I think this is simply a bug in the wording. There is an analogous issue that's arguably a bug with the specification of NULL: it's allowed to be any null pointer constant, where null pointer constant is defined as either:

  1. 值为0的整数常量表达式,或
  2. 这样的表达式强制转换为void *.

但是,为了用作NULL的定义,应在表达式中加上适当的括号;我知道的所有现有实现都可以做到这一点.但是严格来说,(void *)0是空指针常量,而((void *)0)不是(这是括号运算符对空指针常量的应用).

However, to be useful as a definition for NULL, the expression should be properly parenthesized; all existing implementations I'm aware of do this. But by a strict reading, while (void *)0 is a null pointer constant, ((void *)0) is not (it's the application of the parentheses operator to a null pointer constant).

理想情况下,应在规范中的某处添加语言,以阐明括号不会影响此类情况.

Ideally language should be added somewhere in the specification clarifying that parentheses do not affect things like this.

这篇关于关于强制转换为整数常量表达式(在标准C中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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