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

这篇博客探讨了在C++中用于获取当前函数名称的三个魔法变量:__FUNCTION__, __func__, 和 __Pretty_FUNCTION__。尽管__FUNCTION__在旧版本的GCC中广泛使用,但为了更好的移植性,推荐使用C99标准的__func__。在C++中,__FUNCTION__包含函数的类型签名,而不仅仅是名称。在不同的编译器如GCC和MSVC中,还有各自特定的宏来获取函数名。" 88185899,6942416,使用Redis实现广告系统页面广告自动上下线,"['Redis', '广告系统', '系统设计', '缓存管理']

6.47 Function Names as Stringshttp://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 first of these 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.

__FUNCTION__ is another name for __func__. Older versions of GCC recognize only this name. However, it is not standardized. For maximum portability, we recommend you 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__ 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 deprecated
最好使用__func__?
注:__func__是内直变量??

Since __func__ is not a string literal, you can't concatenate it with
another string literal, but what you can do is

printf("%s %s\n", __func__, __FILE__);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值