什么标准调用实际上是宏

What Standard Calls are Actually Macros

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

这给我带来了一个问题,因为 assert 在接受参数的方式上似乎是一个函数:assert(true) 因此我尝试将其用作:std::assert(true) 当然是一个不起作用的宏。

我的问题是:标准库是否提供了任何其他宏,这些宏将显示为带参数的函数?

如果我们查看 [[=​​39=]] 第 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++中也是宏。不过也有一些例外。来自 [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 也涵盖了这些例外情况

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

还有一个例外是7.12.3分类宏中定义的所有分类宏都会被函数重载per [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);