c++ - 无效的静态断言行为

标签 c++ c gcc floating-point

我正在尝试使用 GCC v4.3.x 设置静态断言(在主函数之外):

#define STATIC_ASSERT(cond) extern void static_assert(int arg[(cond) ? 1 : -1])
STATIC_ASSERT( (double)1 == (double)1 ); // failed

但是当我使用 float 时,断言总是失败。

是否可以正确运行此静态断言?​​

最佳答案

C++ 标准 2003,5.19“常量表达式”,第 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.

关于c++ - 无效的静态断言行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1951741/

相关文章:

c++ - gcc:在 C++ 应用程序中链接 C 库导致 "multiple definition of"错误

c++ - 警告 : too many arguments for format - fprintf

c++ - 如何对涉及返回非 void 类型的用户定义类的成员函数的线程使用引用包装 (c++)

c++ - 在字符函数中传递字符值的奇怪错误

c - 如何计算数组中有多少个最大值?

c - GTK3 API : Replacement for deprecated gtk_text_freeze API?

c++ - 定义函数时不给指针或引用变量命名

c++ - Hook dll中的特定函数

c - 通过c中的套接字发送结构

c++ - GCC .data 部分更改