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__. 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\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__(GCC3.4以后版本,此2种东西均为变量). In C++, __FUNCTION__ and__PRETTY_FUNCTION__ have always been variables.

参考:http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html#Function-Names

最新文章

  1. Oracle11g在使用exp导出时不导出空表问题的解决办法
  2. python---sys
  3. [转]比较Jmeter、Grinder和JAVA多线程本身压力测试所带来的性能开销
  4. Weblogic项目部署及数据源配置
  5. Codeforces Burning Midnight Oil
  6. 基于STM32F10x的串口(USART)输入输出编程
  7. Java程序猿JavaScript学习笔记(2——复制和继承财产)
  8. 【HighCharts系列教程】五、版权属性——Credits
  9. angular购物车
  10. Flask类的属性和方法大全
  11. UVALive - 5857 Captain Q&#39;s Treasure
  12. c#如何解析时区字符串
  13. vue教程1-09 交互 vue实现百度下拉列表
  14. 搭建私有yum仓库
  15. fildder教程
  16. 在Windows下将Redis注册为本地服务
  17. react native 0.56.0版本在windows下有bug不能正常运行
  18. Spring security 如何设置才能避免拦截到静态资源
  19. VS2013相关
  20. js数组详解

热门文章

  1. week4d:个人博客作业
  2. typedef struct bit0 : 1
  3. 怎样实现SDO服务
  4. [转帖学习] 使用阿里云证书 升级https
  5. 清理elasticsearch的索引
  6. IDEA 开发工具的快捷键
  7. JDBC数据库连接技术
  8. 016 Java中的动态代理
  9. [NOIP2011]玛雅游戏
  10. HDU.5692 Snacks ( DFS序 线段树维护最大值 )