C++ __PRETTY_FUNCTION__、__FUNCTION__、__func__ 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4384765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:17:03  来源:igfitidea点击:

What's the difference between __PRETTY_FUNCTION__, __FUNCTION__, __func__?

c++ccompiler-constructionstandards

提问by Matt Joiner

What's the difference between __PRETTY_FUNCTION__, __FUNCTION__, __func__, and where are they documented? How do I decide which one to use?

什么之间的区别__PRETTY_FUNCTION____FUNCTION____func__,并且他们记录在哪里呢?我如何决定使用哪一个?

回答by James McNellis

__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__是一个隐式声明的标识符,当它在函数内部使用时,它扩展为包含函数名称的字符数组变量。它在 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 declaration

static 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__由翻译器隐式声明,就好像紧跟在每个函数定义的左大括号之后一样,声明

static const char __func__[] = "function-name";

出现了,其中 function-name 是词法封闭函数的名称。此名称是函数的简单名称。

Note that it is not a macro and it has no special meaning during preprocessing.

请注意,它不是宏,并且在预处理过程中没有特殊含义。

__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).

__func__在 C++11 中被添加到 C++,在那里它被指定为包含“一个实现定义的字符串”(C++11 §8.4.1[dcl.fct.def.general]/8),这不是与 C 中的规范一样有用。(添加__func__到 C++的最初提议是N1642)。

__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__).

__FUNCTION__是一些 C 编译器支持的预标准扩展(包括 gcc 和 Visual C++);通常,您应该__func__在支持它的地方使用它,并且仅__FUNCTION__在您使用不支持它的编译器时才使用(例如,不支持 C99 且尚不支持所有 C++0x 的 Visual C++,不支持提供__func__)。

__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__.

__PRETTY_FUNCTION__是一个 gcc 扩展,与 几乎相同__FUNCTION__,除了对于 C++ 函数,它包含函数的“漂亮”名称,包括函数的签名。Visual C++ 有一个类似(但不完全相同)的扩展名__FUNCSIG__.

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."

对于非标准宏,您需要查阅编译器的文档。Visual C++ 扩展包含在 C++ 编译器的“预定义宏”的 MSDN 文档中。gcc 文档扩展在 gcc 文档页面“函数名称作为字符串”中进行了描述

回答by Petr

Despite not fully answering the original question, this is probably what most people googling this wanted to see.

尽管没有完全回答最初的问题,但这可能是大多数使用谷歌搜索的人想要看到的。

For GCC:

对于海湾合作委员会:

petanb@debian:~$ cat test.cpp 
#include <iostream>

int main(int argc, char **argv)
{
    std::cout << __func__ << std::endl
              << __FUNCTION__ << std::endl
              << __PRETTY_FUNCTION__ << std::endl;
}
petanb@debian:~$ g++ test.cpp 
petanb@debian:~$ 
petanb@debian:~$ ./a.out 
main
main
int main(int, char**)

回答by sashang

__func__is documented in the C++0x standard at section 8.4.1. In this case it's a predefined function local variable of the form:

__func__在第 8.4.1 节的 C++0x 标准中进行了记录。在这种情况下,它是以下形式的预定义函数局部变量:

static const char __func__[] = "function-name ";

where "function name" is implementation specfic. This means that whenever you declare a function, the compiler will add this variable implicitly to your function. The same is true of __FUNCTION__and __PRETTY_FUNCTION__. Despite their uppercasing, they aren't macros. Although __func__is an addition to C++0x

其中“函数名称”是实现特定的。这意味着每当你声明一个函数时,编译器都会隐式地将这个变量添加到你的函数中。__FUNCTION__和也是如此__PRETTY_FUNCTION__。尽管它们大写,但它们不是宏。虽然__func__是 C++0x 的补充

g++ -std=c++98 ....

will still compile code using __func__.

仍将使用__func__.

__PRETTY_FUNCTION__and __FUNCTION__are documented here http://gcc.gnu.org/onlinedocs/gcc-4.5.1/gcc/Function-Names.html#Function-Names. __FUNCTION__is just another name for __func__. __PRETTY_FUNCTION__is the same as __func__in C but in C++ it contains the type signature as well.

__PRETTY_FUNCTION____FUNCTION__在此处记录http://gcc.gnu.org/onlinedocs/gcc-4.5.1/gcc/Function-Names.html#Function-Names__FUNCTION__只是 的另一个名称__func____PRETTY_FUNCTION____func__C 中相同,但在 C++ 中它也包含类型签名。

回答by finnan

For those, who wonder how it goes in VS.

对于那些想知道它在 VS 中如何进行的人。

MSVC 2015 Update 1, cl.exe version 19.00.24215.1:

MSVC 2015 更新 1,cl.exe 版本 19.00.24215.1:

#include <iostream>

template<typename X, typename Y>
struct A
{
  template<typename Z>
  static void f()
  {
    std::cout << "from A::f():" << std::endl
      << __FUNCTION__ << std::endl
      << __func__ << std::endl
      << __FUNCSIG__ << std::endl;
  }
};

void main()
{
  std::cout << "from main():" << std::endl
    << __FUNCTION__ << std::endl
    << __func__ << std::endl
    << __FUNCSIG__ << std::endl << std::endl;

  A<int, float>::f<bool>();
}

output:

输出:

from main():
main
main
int __cdecl main(void)

from A::f():
A<int,float>::f
f
void __cdecl A<int,float>::f<bool>(void)

Using of __PRETTY_FUNCTION__triggers undeclared identifier error, as expected.

__PRETTY_FUNCTION__正如预期的那样,使用触发器未声明的标识符错误。