__PRETTY_FUNCTION__、__FUNCTION__、__func__ 有什么区别?
__PRETTY_FUNCTION__
、__FUNCTION__
、__func__
之间有什么区别,它们的文档记录在哪里?我如何决定使用哪一个?
What's the difference between __PRETTY_FUNCTION__
, __FUNCTION__
, __func__
, and where are they documented? How do I decide which one to use?
推荐答案
__func__
是一个隐式声明的标识符,当它在函数内部使用时扩展为包含函数名称的字符数组变量.它在 C99 中被添加到 C 中.来自 C99 §6.4.2.2/1:
__func__
is an implicitly declared identifier that expands to a character array variable containing the function name when it is used inside of a function. It was added to C in C99. From C99 §6.4.2.2/1:
标识符 __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.
注意,它不是宏,在预处理过程中没有特殊意义.
Note that it is not a macro and it has no special meaning during preprocessing.
__func__
在 C++11 中被添加到 C++ 中,其中它被指定为包含一个实现定义的字符串".(C++11 §8.4.1[dcl.fct.def.general]/8),它不如 C 中的规范有用.(将 __func__
添加到 C++ 的原始提议是 N1642).
__func__
was added to C++ in C++11, where it is specified as containing "an implementation-de?ned string" (C++11 §8.4.1[dcl.fct.def.general]/8), which is not quite as useful as the specification in C. (The original proposal to add __func__
to C++ was N1642).
__FUNCTION__
是一些 C 编译器支持的预标准扩展(包括 gcc 和 Visual C++);通常,您应该在支持的地方使用 __func__
,并且仅在使用不支持它的编译器时才使用 __FUNCTION__
(例如,Visual C++,它不支持支持C99,尚不支持所有C++0x,不提供__func__
).
__FUNCTION__
is a pre-standard extension that some C compilers support (including gcc and Visual C++); in general, you should use __func__
where it is supported and only use __FUNCTION__
if you are using a compiler that does not support it (for example, Visual C++, which does not support C99 and does not yet support all of C++0x, does not provide __func__
).
__PRETTY_FUNCTION__
是一个 gcc 扩展,与 __FUNCTION__
基本相同,只是对于 C++ 函数,它包含漂亮"的;函数的名称,包括函数的签名.Visual C++ 有一个类似(但不完全相同)的扩展,__FUNCSIG__
.
__PRETTY_FUNCTION__
is a gcc extension that is mostly the same as __FUNCTION__
, except that for C++ functions it contains the "pretty" name of the function including the signature of the function. Visual C++ has a similar (but not quite identical) extension, __FUNCSIG__
.
对于非标准宏,您需要查阅编译器的文档.Visual C++ 扩展包含在 C++ 编译器 "Predefined Macros" 的 MSDN 文档中.gcc 文档扩展在 gcc 文档页面 "Function Names as Strings" 中进行了描述.;
For the nonstandard macros, you will want to consult your compiler's documentation. The Visual C++ extensions are included in the MSDN documentation of the C++ compiler's "Predefined Macros". The gcc documentation extensions are described in the gcc documentation page "Function Names as Strings."
相关文章