__PRETTY_FUNCTION__
、__FUNCTION__
、__func__
之间有什么区别,它们在哪里记录?我如何决定使用哪一个?
最佳答案
__func__
是一个隐式声明的标识符,当它在函数内部使用时,它会扩展为包含函数名称的字符数组变量。它在 C99 中被添加到 C 中。来自 C99 §6.4.2.2/1:
The identifier
__func__
is implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declarationstatic const char __func__[] = "function-name";
appeared, where function-name is the name of the lexically-enclosing function. This name is the unadorned name of the function.
注意,它不是宏,在预处理过程中没有特殊意义。
__func__
在 C++11 中被添加到 C++,其中它被指定为包含“一个实现定义的字符串”(C++11 §8.4.1[dcl.fct.def. general]/8),它不如 C 中的规范有用。(将 __func__
添加到 C++ 的最初提议是 N1642 )。
__FUNCTION__
是一些 C 编译器支持的预标准扩展(包括 gcc 和 Visual C++);通常,您应该在支持的地方使用 __func__
,并且仅在使用不支持它的编译器时才使用 __FUNCTION__
(例如,Visual C++,它不支持支持C99,尚不支持所有C++0x,不提供__func__
)。
__PRETTY_FUNCTION__
是一个 gcc 扩展,与 __FUNCTION__
基本相同,除了对于 C++ 函数,它包含函数的“漂亮”名称,包括函数的签名功能。 Visual C++ 有一个类似(但不完全相同)的扩展,__FUNCSIG__
。
对于非标准宏,您需要查阅编译器的文档。 Visual C++ 扩展包含在 C++ 编译器的 "Predefined Macros" 的 MSDN 文档中。 . gcc 文档扩展在 gcc 文档页面 "Function Names as Strings." 中进行了描述。
关于c++ - __PRETTY_FUNCTION__、__FUNCTION__、__func__ 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4384765/