Original address: C + + library research notes--macro definition of function name
6.47 Function Names as strings:http://gcc.gnu.org/onlinedocs/gcc/function-names.html
GCC provides three magic variables that hold the name of the current function, as a string. The "A" is __func__, which are part of the C99 standard:
The identifier __func__ is implicitly declared by the translator as if, immediately following the opening brace of each Fu Nction 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.
__FUNCTION__ is another name for __func__. Older versions of GCC recognize only this name. However, it is not standardized. For maximum portability, we recommend your use __func__, but provide a fallback definition with the preprocessor:
#if __stdc_version__ < 199901L
# if __gnuc__ >= 2
# define __func__ __function__
# Else
# Define __FUNC__ "<unknown>"
# endif
#endif
In C, __pretty_function__ are yet another name for __func__. However, in C + +, __PRETTY_FUNCTION__ contains the type signature of the FUNCTION as, as its bare name. For example:
extern "C" {
extern int printf (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 are not preprocessor macros. In GCC 3.3 and earlier, in C only, __function__ and __pretty_function__ were treated as string literals; They could is used to initialize char arrays, and they could is concatenated with the other string literals. GCC 3.4 and later treat them as variables, like __func__. In C + +, __function__ and __pretty_function__ have always been variables.
But, according to, __function__ more insurance.
Macro/keyword which can be used to print out method name?
On GCC for you can use __function__ and __pretty_function__. On MSVC can use __funcsig__ and __function__. simple.c:in function ' main ':
Simple.c:8: Warning:concatenation of string literals with
__function__ is Deprecated
Print the function name using __FUNCTION__ macro summarizes the data: it is best to use __func__. Note: __func__ is an internal straight variable.
Since __func__ is isn't a string literal, you can ' t concatenate it with
another string literal, but what your can do is
printf ("%s%s\n", __func__, __file__);