Function Names as Strings

2825 단어 function
【Function Names as Strings】
GCC provides three magic variables that hold the name of the current function, as a string. The first of these is  __func__ , which is part of the C99 standard(old):
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. __FUNCTION__  is another name for  __func__ . GCC의 Older versions of GCC recognize only this name(GCC2.0 이상 버전에는 __func___가 없고 __FUNCTION__만 있으며 2.0 이하 버전 컴파일러는 __FUNCTION___를 모릅니다.However, it is not standardized. For maximum portability, we recommend you use__func__, but provide a fallback definition with the preprocessor(GCC2.0)(이하 매크로를 통해 _func___를 사용하면 크로스 버전 특성을 제공할 수 있음):
     #if __STDC_VERSION__ < 199901L

     # if __GNUC__ >= 2

     #  define __func__ __FUNCTION__

     # else

     #  define __func__ "<unknown>"

     # endif

     #endif


In C,  __PRETTY_FUNCTION__  is yet another name for  __func__ . However, in C++, __PRETTY_FUNCTION__ contains the type signature of the function as well as its bare name(C++에서 _PRETTY_FUNCTION__함수 서명 포함).For example, this program:
     extern "C" {

     extern int printf (char *, ...);

     }

     

     class a {

      public:

       void sub (int i)

         {

           printf ("__FUNCTION__ = %s
", __FUNCTION__); printf ("__PRETTY_FUNCTION__ = %s
", __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 not preprocessor macros. In GCC 3.3 and earlier, in C only,  __FUNCTION__  and  __PRETTY_FUNCTION__  were treated as string literals; they could be used to initialize  char  arrays, and they could be concatenated with other string literals. GCC 3.4 and later treat them as variables, like  __func__(GCC3.4 , 2 . In C++,  __FUNCTION__  and __PRETTY_FUNCTION__  have always been variables.
참조: http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html#Function-Names

좋은 웹페이지 즐겨찾기