C标准是否有任何允许将函数实现为宏的限制? [英] Are there any restrictions to the C standard allowing functions to be implemented as macros?

查看:79
本文介绍了C标准是否有任何允许将函数实现为宏的限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,除了提供函数声明外,C标准标头还可以提供掩蔽宏"以使处理速度更快.例如,如果包含ctype.h,则头文件将声明

Often, in addition to providing a function declaration, C standard headers may provide a "masking macro" to make things speedier. For example, if I include ctype.h, the header file will declare

int isdigit(int c);

但是它也可以用宏掩盖声明.我相信这是根据C标准的可移植isdigit宏:

But it may also mask the declaration with a macro. I believe this is a portable isdigit macro according to the C standard:

#define isdigit(c) ((c) >= '0' && (c) <= '9')

当然,此宏也很危险,因为如果您在定义宏时执行此操作,则会引入未定义的行为:

Of course, this macro is also dangerous because it introduces undefined behavior if you do this while the macro is defined:

int c = 'A';
printf("%d\n", isdigit(c++));

在这种假设的情况下,为了避免使用UB,我必须用括号括住函数名称:(isdigit)(c++).因此,我的问题是:标准标头可以定义哪种屏蔽宏有什么限制?如果参数表达式具有副作用,是否保证它们不会引起未定义的行为,或者从技术上讲,它们是否被允许具有上述奇怪的行为?限制在哪里?

To avoid UB in this hypothetical case, I have to surround the function name with parens: (isdigit)(c++). So, my question is: are there any restrictions to what sort of masking macros a standard header can define? Are they guaranteed to not cause undefined behavior if an argument expression has side effects, or are they technically allowed to have weird behavior such as we see above? Where are the limits?

推荐答案

根据C11 7.1.4.1,使用库函数",尤其是下面引用的最后一部分:

Per C11 7.1.4.1, "Use of Library Functions", particularly the last part quoted below:

标头中声明的任何函数都可以另外实现为 标头中定义了类似函数的宏,因此如果是库函数 在包含其标头时明确声明,该标头之一 可以使用以下所示的技术来确保不声明 受这样的宏的影响....出于相同的语法原因,它是 允许使用库函数的地址,即使它也是 定义为宏.使用#undef删除任何宏定义 还将确保引用实际功能.任何 调用实现为宏的库函数 扩展为仅对每个参数一次求值的代码, 必要时用括号完全保护,因此通常 使用任意表达式作为参数很安全.

Any function declared in a header may be additionally implemented as a function-like macro defined in the header, so if a library function is declared explicitly when its header is included, one of the techniques shown below can be used to ensure the declaration is not affected by such a macro....For the same syntactic reason, it is permitted to take the address of a library function even if it is also defined as a macro. The use of #undef to remove any macro definition will also ensure that an actual function is referred to. Any invocation of a library function that is implemented as a macro shall expand to code that evaluates each of its arguments exactly once, fully protected by parentheses where necessary, so it is generally safe to use arbitrary expressions as arguments.

请注意,结尾处的通常"很重要,并且存在明确的例外情况. C11 7.21.7.5.2说:"getc函数等效于fgetc,不同之处在于,如果将其实现为宏,则它可能对stream进行多次计算,因此该参数永远不应是带有side的表达式.效果",在C11 7.21.7.7.2中使用与putc类似的语言.在这种特殊情况下,streamFILE *,因此在正常情况下,将此作为具有副作用的表达式会有些奇怪,但是可能会发生.对于宽字符对应的putwc()getwc(),也是如此.我不知道其他类似的例外情况.

Note that the "generally" at the end is important, there, and there are explicit exceptions. C11 7.21.7.5.2 says "the getc function is equivalent to fgetc, except that if it is implemented as a macro, it may evaluate stream more than once, so the argument should never be an expression with side effects", with similar language for putc in C11 7.21.7.7.2. In this particular case, stream is a FILE *, so under normal circumstances it would be kind of weird to have this as an expression with side-effects, but it could happen. The same is also true for their wide character counterparts, putwc() and getwc(). I am not aware of any other exceptions like this.

这篇关于C标准是否有任何允许将函数实现为宏的限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆