gcc: Function Names
1
1 6.48 Function Names as Strings
1 ==============================
1
1 GCC provides three magic constants that hold the name of the current
1 function as a string. In C++11 and later modes, all three are treated
1 as constant expressions and can be used in 'constexpr' constexts. The
1 first of these constants is '__func__', which is part of the C99
1 standard:
1
1 The identifier '__func__' is implicitly declared by the translator as
1 if, immediately following the opening brace of each function definition,
1 the declaration
1
1 static const char __func__[] = "function-name";
1
1 appeared, where function-name is the name of the lexically-enclosing
1 function. This name is the unadorned name of the function. As an
1 extension, at file (or, in C++, namespace scope), '__func__' evaluates
1 to the empty string.
1
1 '__FUNCTION__' is another name for '__func__', provided for backward
1 compatibility with old versions of GCC.
1
1 In C, '__PRETTY_FUNCTION__' is yet another name for '__func__', except
1 that at file (or, in C++, namespace scope), it evaluates to the string
1 '"top level"'. In addition, in C++, '__PRETTY_FUNCTION__' contains the
1 signature of the function as well as its bare name. For example, this
1 program:
1
1 extern "C" int printf (const char *, ...);
1
1 class a {
1 public:
1 void sub (int i)
1 {
1 printf ("__FUNCTION__ = %s\n", __FUNCTION__);
1 printf ("__PRETTY_FUNCTION__ = %s\n", __PRETTY_FUNCTION__);
1 }
1 };
1
1 int
1 main (void)
1 {
1 a ax;
1 ax.sub (0);
1 return 0;
1 }
1
1 gives this output:
1
1 __FUNCTION__ = sub
1 __PRETTY_FUNCTION__ = void a::sub(int)
1
1 These identifiers are variables, not preprocessor macros, and may not
1 be used to initialize 'char' arrays or be concatenated with string
1 literals.
1