__func__未在预处理输出中替换 [英] __func__ not replaced in preprocessed output
问题描述
我正在阅读有关C/C ++中的__FUNCTION__
/__func__
的信息(它们用于打印使用它们的函数的名称).我读过的每个地方都说它们是宏,在预处理时会被替换.因此,我通过查看使用命令gcc -E prog.c
的预处理输出来对此进行了调查.但是我发现__func__
和__FUNCTION__
都没有被函数名称的预处理器替换.
I was reading about __FUNCTION__
/ __func__
in C/C++ (they are used to print the name of the function in which they are used). Every place I read they said that these are macros and are replaced at preprocessing time. So, I investigated this by seeing the preprocessed output using command gcc -E prog.c
. But I saw that neither __func__
nor __FUNCTION__
were replaced by the preprocessor by the function name.
那么,它是一个宏吗?如果没有,它是什么以及如何实现?
So, is it a macro? If not, what is it and how is it implemented?
编辑
甚至尝试过cpp prog.c
.但仍然没有被替换.
Even tried cpp prog.c
. But still not replaced.
还 __FILE __,__ LINE__和__FUNCTION__在C ++中的用法这篇文章说,它从不影响性能.请澄清.
Also __FILE__, __LINE__, and __FUNCTION__ usage in C++ this post says that it never affects performance. Please clarify.
推荐答案
它们被实现为魔术变量". 手册说:
They are implemented as (and are) "magic variables". The manual says:
GCC提供了三个魔术变量,它们以字符串形式保存当前函数的名称. 其中的第一个是
__func__
,它是C99标准的一部分:
GCC provides three magic variables that hold the name of the current function, as a string. The first of these is
__func__
, which is part of the C99 standard:
标识符__func__
由翻译器隐式声明,就像立即
按照每个函数定义的开头括号,声明
The identifier __func__
is implicitly declared by the translator as if, immediately
following the opening brace of each function definition, the declaration
static const char __func__[] = "function-name";
出现
,其中function-name
是词法包围函数的名称.这个名字
是该函数的未经修饰的名称.
appeared, where function-name
is the name of the lexically-enclosing function. This name
is the unadorned name of the function.
它们通常不能实现为预处理器宏;预处理器不解析函数作用域.当然,可以使预处理器 足够了解语法,以了解函数的开始和结束位置,但是通常它们不在该级别上运行.
They typically cannot be implemented as preprocessor macros; the preprocessor doesn't parse function scopes. Of course a preprocessor could be made to understand enough of the syntax to know where functions begin and end, but typically they're not operating at that level.
这篇关于__func__未在预处理输出中替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!