【发布时间】:2022-01-10 05:07:23
【问题描述】:
我不明白宏不只评估一次参数(如内联函数)而是每次在代码中使用的原因是什么。
例如:
(示例取自这里:https://docs.microsoft.com/en-us/cpp/cpp/inline-functions-cpp?view=msvc-170)
在这段代码中有一个宏:
// inline_functions_macro.c
#include <stdio.h>
#include <conio.h>
#define toupper(a) ((a) >= 'a' && ((a) <= 'z') ? ((a)-('a'-'A')):(a))
int main() {
char ch;
printf_s("Enter a character: ");
ch = toupper( getc(stdin) );
printf_s( "%c", ch );
}
// Sample Input: xyz
// Sample Output: Z
与此代码相比,具有等效的内联函数:
// inline_functions_inline.cpp
#include <stdio.h>
#include <conio.h>
inline char toupper( char a ) {
return ((a >= 'a' && a <= 'z') ? a-('a'-'A') : a );
}
int main() {
printf_s("Enter a character: ");
char ch = toupper( getc(stdin) );
printf_s( "%c", ch );
}
【问题讨论】:
-
宏是文本替换。它们不是函数。
-
宏不是函数。它们是预处理器文本替换,仅此而已。不多也不少。就像您的源代码实际上具有由预处理器完成的替换文本,因此编译器抱怨每个宏的使用,而不是宏本身。
-
一个重要的理解是宏是在编译之前展开的,也就是通常所说的“预处理阶段”。
-
尝试使用以下参数扩展宏,“hello”和 3.1415269。这应该会告诉你为什么函数比宏更受欢迎。