无效的静态断言行为 [英] invalid static assert behavior
问题描述
我试图用GCC v4.3.x设置一个静态断言(在主函数之外):
#定义STATIC_ASSERT(cond)extern void static_assert(int arg [(cond)?1:-1])$ b $ b STATIC_ASSERT((double)1 ==(double)1); //失败
但是当我使用浮点数时,断言总是失败。
$ b
是否可以正确运行此静态断言?
有几个地方,C ++需要
表达式,
整数或枚举常量:为
数组边界(8.3.4,5.3.4),如
表达式(6.4.2)作为静态成员
初始值设定项(9.4.2),作为整数
或枚举非模板
参数(14.3)。
常量表达式:
条件表达式
一个整型常量表达式可以只包含文本(2.13),
枚举器,常量变量或静态
数据成员整数或
使用
常量表达式(8.5),非类型
模板参数或
枚举类型和sizeof
表达式初始化的枚举类型。 浮点文字
(2.13.3)只有在
转换为整型或枚举类型时才会出现。
仅转换为整型或
枚举类型可以使用。在
中,除了sizeof
表达式,函数,类对象,
指针或引用不能使用
外,赋值,增量,
递减,函数-ball或逗号
操作员不得使用。
I am trying to setup a static assert (outside the main function) with GCC v4.3.x:
#define STATIC_ASSERT(cond) extern void static_assert(int arg[(cond) ? 1 : -1])
STATIC_ASSERT( (double)1 == (double)1 ); // failed
but when I use float numbers, the assert always failed.
Is it possible to run this static assert properly ?
C++ Standard 2003, 5.19 "Constant expressions", paragraph 1.
In several places, C++ requires expressions that evaluate to an integral or enumeration constant: as array bounds (8.3.4, 5.3.4), as case expressions (6.4.2), as bit-field lengths (9.6), as enumerator initializers (7.2), as static member initializers (9.4.2), and as integral or enumeration non-type template arguments (14.3).
constant-expression: conditional-expression
An integral constant-expression can involve only literals (2.13), enumerators, const variables or static data members of integral or enumeration types initialized with constant expressions (8.5), non-type tem- plate parameters of integral or enumeration types, and sizeof expressions. Floating literals (2.13.3) can appear only if they are cast to integral or enumeration types. Only type conversions to integral or enumeration types can be used. In particular, except in sizeof expressions, functions, class objects, pointers, or references shall not be used, and assignment, increment, decrement, function-call, or comma operators shall not be used.
这篇关于无效的静态断言行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!