为什么某些库例程同时实现为宏?为什么是"va_arg"?宏声明为函数(没有"#define")? [英] Why are some library routines implemented as macros simultaneously? Why is "va_arg" macro declared as a function (without "#define")?
问题描述
我很难用言语清楚地表达出来.因此,让我将其分为几部分.上下文摘自Mike Banahan的C书(下面各部分提供的链接).这是我的问题,用粗体字表示:
I am struggling to put it clearly in words. So let me put it in parts. The contexts are from the C book by Mike Banahan (Links provided with each part below). Here are my questions as bullet points in bold:
- 为什么某些库函数也同时实现为宏?有什么需要这就是我从《 (第9.1.1节):
最后一个一般要点是,许多库例程可能是 实现为宏,前提是没有问题要做 有副作用(如第7章所述).标准保证 如果正常情况下将函数实现为宏,则将 提供的真正功能也可以完成相同的工作.使用真实 函数,或者用 #undef 取消定义宏名称,或将其括起来 括号中的名称,以确保不会将其视为 宏:
A last general point is that many of the library routines may be implemented as macros, provided that there will be no problems to do with side-effects (as Chapter 7 describes). The Standard guarantees that, if a function is normally implemented as a macro, there will also be a true function provided to do the same job. To use the real function, either undefine the macro name with #undef, or enclose its name in parentheses, which ensures that it won't be treated as a macro:
-
众所周知,
- va_start 是函数还是宏?书中的以下内容是混淆的根源,因为它暗示着彼此在同一口气中,在相邻的行中! (第9.9节)
- va_start, as we know it, is a function or macro? The following text from the book is the source of confusion as it implies both in the same breath, in adjacent lines!! (Section 9.9)
在尝试访问变量参数列表之前, 必须调用 va_start .定义为
Before any attempt can be made to access a variable argument list, va_start must be called. It is defined as
#include <stdarg.h>
void va_start(va_list ap, parmN);
va_start 宏会初始化ap,以供函数随后使用 va_arg 和 va_end .
The va_start macro initializes ap for subsequent use by the functions va_arg and va_end.
- 最后,最令人困惑的部分.在下面的行中,清楚地写出 va_arg 是一个宏,然后继续说明它是如何实现的.但是,如何在没有 #define 关键字的情况下实现宏,而对于带有返回类型('type')的宏也是如此,就好像它是一个函数一样? (第9.9节)
- And finally, the most confusing part. In the following line, it's clearly written that va_arg is a macro and goes on to show how it is implemented. But how can a macro be implemented without a #define keyword, and that too with return type ('type') as if it were a function? (Section 9.9)
初始化后,提供的参数可以顺序访问 通过 va_arg 宏.这是特殊的,因为类型 返回的值由宏的参数确定.请注意,这是 不可能实现为真正的功能,只能实现为宏.它是 定义为
Once initialized, the arguments supplied can be accessed sequentially by means of the va_arg macro. This is peculiar because the type returned is determined by an argument to the macro. Note that this is impossible to implement as a true function, only as a macro. It is defined as
#include <stdarg.h>
type va_arg(va_list ap, type);
非常感谢您的回答.谢谢.
Your answers will be very much appreciated. Thank you.
推荐答案
这是两个不同的问题.
首先,保证va_start
,va_arg
和va_end
是宏. va_arg
不能是一个函数,因为它的第二个参数是类型而不是值.
Firstly, va_start
, va_arg
and va_end
are guaranteed to be macros. va_arg
cannot be a function, because its second argument is a type rather than a value.
关于为什么某些函数也是宏的原因:因为您希望内联一个调用,使调用快速进行,但是您可能还想获取其地址并将其放在函数指针中:
As to why some functions are also macros: because you want a call to be inlined, making it fast, but you might also want to take its address and put that in a function pointer:
int (*function_pointer)(int, FILE *) = &(putc);
这篇关于为什么某些库例程同时实现为宏?为什么是"va_arg"?宏声明为函数(没有"#define")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!