senby 发表于 2013-8-12 11:31:15

C++库研究笔记——函数名的宏定义

   #endifIn 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. For example, this program:   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 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__. In C++, __FUNCTION__ and __PRETTY_FUNCTION__ have always been variables.
但是,根据,__FUNCTION__更保险?Macro / keyword which can be used to print out method name?
On GCC you can use __FUNCTION__ and __PRETTY_FUNCTION__.On MSVC you can use __FUNCSIG__ and __FUNCTION__.simple.c: In function `main':simple.c:8: warning: concatenation of string literals with__FUNCTION__ is deprecatedPrint the function name using __FUNCTION__ macro
总结各资料:最好使用__func__?注:__func__是内直变量??
Since __func__ is not a string literal, you can't concatenate it withanother string literal, but what you can do is
printf("%s %s\n", __func__, __FILE__);
页: [1]
查看完整版本: C++库研究笔记——函数名的宏定义