Function Names (Using the GNU Compiler Collection (GCC)) (original) (raw)


6.12.22 Function Names as Strings

GCC provides three magic constants that hold the name of the current function as a string. In C++11 and later modes, all three are treated as constant expressions and can be used in constexpr constexts. The first of these constants is __func__, which is part of the C99 standard:

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. As an extension, at file (or, in C++, namespace scope), __func__evaluates to the empty string.

__FUNCTION__ is another name for __func__, provided for backward compatibility with old versions of GCC.

In C, __PRETTY_FUNCTION__ is yet another name for__func__, except that at file scope (or, in C++, namespace scope), it evaluates to the string "top level". In addition, in C++,__PRETTY_FUNCTION__ contains the signature of the function as well as its bare name. For example, this program:

extern "C" int printf (const char *, ...);

class a { public: void sub (int i) { printf ("FUNCTION = %s\n", FUNCTION); printf ("PRETTY_FUNCTION = %s\n", PRETTY_FUNCTION); } };

int main (void) { a ax; ax.sub (0); return 0; }

gives this output:

FUNCTION = sub PRETTY_FUNCTION = void a::sub(int)

These identifiers are variables, not preprocessor macros, and may not be used to initialize char arrays or be concatenated with string literals.