c++ - 什么标准调用实际上是宏

标签 c++ parameters macros namespaces assert

我问了一个问题here关于assert它在标准中作为宏实现,而不是函数。

这给我带来了一个问题,因为 assert 似乎是一个函数,它采用参数:assert(true) 因此我尝试使用它是:std::assert(true),当然这是一个不起作用的宏。

我的问题是:标准库是否提供了任何其他宏,它们会显示为带有参数的函数?

最佳答案

如果我们看一下 [headers] 第 5 段和第 6 段,我们会发现

Names which are defined as macros in C shall be defined as macros in the C++ standard library, even if C grants license for implementation as functions. [ Note: The names defined as macros in C include the following: assert, offsetof, setjmp, va_arg, va_end, and va_start. —end note ]

Names that are defined as functions in C shall be defined as functions in the C++ standard library.

因此,如果它在 C 中定义为宏,那么它在 C++ 中也将是宏。但也有一些异常(exception)。来自 [support.runtime] 第 7 和 8 段

The header <cstdalign> and the header <stdalign.h> shall not define a macro named alignas.

The header <cstdbool> and the header <stdbool.h> shall not define macros named bool, true, or false.

尽管 [headers]/7 也涵盖了这些异常(exception)情况

Identifiers that are keywords or operators in C++ shall not be defined as macros in C++ standard library headers.

还有一个异常(exception),即 7.12.3 分类宏中定义的所有分类宏都可以按 [c.math]/10 的函数重载

The classification/comparison functions behave the same as the C macros with the corresponding names defined in 7.12.3, Classification macros, and 7.12.14, Comparison macros in the C Standard. Each function is overloaded for the three floating-point types, as follows:

int fpclassify(float x);
bool isfinite(float x);
bool isinf(float x);
bool isnan(float x);
bool isnormal(float x);
bool signbit(float x);
bool isgreater(float x, float y);
bool isgreaterequal(float x, float y);
bool isless(float x, float y);
bool islessequal(float x, float y);
bool islessgreater(float x, float y);
bool isunordered(float x, float y);
int fpclassify(double x);
bool isfinite(double x);
bool isinf(double x);
bool isnan(double x);
bool isnormal(double x);
bool signbit(double x);
bool isgreater(double x, double y);
bool isgreaterequal(double x, double y);
bool isless(double x, double y);
bool islessequal(double x, double y);
bool islessgreater(double x, double y);
bool isunordered(double x, double y);
int fpclassify(long double x);
bool isfinite(long double x);
bool isinf(long double x);
bool isnan(long double x);
bool isnormal(long double x);
bool signbit(long double x);
bool isgreater(long double x, long double y);
bool isgreaterequal(long double x, long double y);
bool isless(long double x, long double y);
bool islessequal(long double x, long double y);
bool islessgreater(long double x, long double y);
bool isunordered(long double x, long double y);

关于c++ - 什么标准调用实际上是宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44369648/

相关文章:

javascript - 如何从Ajax返回字符串中解析参数

ios - 通过选择器发送多个参数?

macros - 无法使用 rsfuzzy crate 中的宏

android - 如何在Android Studio中扩展C++宏?

macros - GTM 减少标签数量

c++ - 如何判断在 Windows Mobile 6.5 中按下某个键 (c++)

c++ - 是否可以在运行时定义要在 STXXL 中排序的类型的长度?

c++ - 逐行遍历char数组的最佳方法

c++ - 雪豹上的 python/c++ 包装

c# - 重用方法参数