gpt4 book ai didi

c - ifndef 是否总是在用空替换列表定义的类对象宏上工作

转载 作者:太空宇宙 更新时间:2023-11-04 07:27:09 25 4
gpt4 key购买 nike

Include guards在头文件中通常用于保护代码部分免受双重包含:

#ifndef FOOBAR_H
#define FOOBAR_H

extern void myfoofunc(void);

#endif

包含守卫通常依赖于这样的期望:如果已经定义了类似对象的宏,则不会包含 #ifndef block 中的行——从而避免双重包含。

我注意到许多包含 header 的 #define 行都有空的替换列表。 C99 标准是否保证用空替换列表定义的类对象宏将被 #ifndef 视为“已定义”?

在描述#define 的语法时,C99 标准似乎暗示6.10.3 节第9 段 中需要替换列表:

A preprocessing directive of the form

# define identifier replacement-list new-line

defines an object-like macro that causes each subsequent instance of the macro name to be replaced by the replacement list of preprocessing tokens that constitute the remainder of the directive. The replacement list is then rescanned for more macro names as specified below.

这是否意味着包含 header 应该采用以下形式:#define FOOBAR_H 1

最佳答案

replacement-list 的标准语法生成是:pp-tokens [opt]。因此,替换列表的有效性不需要 token 。

因此 #ifdef 将正常工作,并且对于定义为“空”的宏来说符合预期。大量代码都依赖于此。

关于c - ifndef 是否总是在用空替换列表定义的类对象宏上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17352973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com