为什么库要为 C++ 关键字创建宏别名?

Why do libraries create macro aliases for c++ keywords?

我经常在大型库中看到类似于以下的代码:

#define MY_CONSTEXPR constexpr
#define MY_NOEXCEPT noexcept
#define MY_NODISCARD [[nodiscard]]
etc.

为这些关键字创建别名的 purpose/benefit 是什么?我经常看到这种情况,但找不到任何关于这种做法的信息。如果我不得不猜测,原因是您可以有条件地编译是否希望这些关键字出现在您的编译中。

What is the purpose/benefit to creating aliases for these keywords? I see this fairly commonly but couldn't find anything regarding the practice. If I had to guess, the reason is so you can conditionally compile whether you want these keywords present in your compilation.

是的,通常如此。您可能还会看到带有空替换文本的类似定义,例如

#define MY_CONST

,涵盖了您不希望使用关键字的情况。

更大的图景是,这有助于与各种语言版本和各种编译器兼容。这样的定义可以在构建库时编写(通常适用于用于构建它的工具链),或者它们可以包含在条件编译指令中,这些指令试图在客户端程序运行时决定如何定义宏。已编译。