What's the difference between __PRETTY_FUNCTION__
, __FUNCTION__
, __func__
, and where are they documented? How do I decide which one to use?
相关问题
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
__PRETTY_FUNCTION__
handles C++ features: classes, namespaces, templates and overloadOutput GCC 7.2
g++ -std=gnu++98
:For those, who wonder how it goes in VS.
MSVC 2015 Update 1, cl.exe version 19.00.24215.1:
output:
Using of
__PRETTY_FUNCTION__
triggers undeclared identifier error, as expected.__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: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++0xwill still compile code using
__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.__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: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-defined 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__
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__
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__
.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."
Despite this doesn't fully answer the original question, it's what most of people who google this might wanted to see
For GCC: