问题描述
我想写一个C宏这需要这样的:
I'd like to write a C macro which takes this:
int foo() {
MY_MACRO
}
和它扩展到这一点:
int foo() {
_macro_var_foo++;
}
我发现我不能使用 __ __ FUNC
,因为这实际上并没有得到在宏扩大;它是由preprocessor像一个变量处理。
I've found that I can't use __func__
, because that doesn't actually get expanded in the macro; it's treated by the preprocessor like a variable.
有没有办法让这个工作?
Is there some way to get this to work?
推荐答案
在preprocessor不知道的功能,只是源文件和行号。在这个阶段它不执行语法分析,只是文本分析和替换。这就是为什么 __ FUNC __
是一个神奇的变量而不是一个神奇的宏观像 __ FILE __
和 __ LINE __
。
The preprocessor doesn't know about functions, just source files and line numbers. At that stage it's not performing syntactical analysis, just textual analysis and substitutions. That's why __func__
is a magical variable instead of a magical macro like __FILE__
and __LINE__
.
这篇关于我完全可以替代__func__成一个标识符名称在C宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!