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

Function Names as Strings的更多相关文章

  1. GNU :6.47 Function Names as Strings

    链接:http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html#Function-Names GCC provides three magic var ...

  2. REST Representational state transfer REST Resource Naming Guide Never use CRUD function names in URIs

    怎样用通俗的语言解释什么叫 REST,以及什么是 RESTful? - 知乎  https://www.zhihu.com/question/28557115 大家都知道"古代"网 ...

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

    6.47 Function Names as Strings:http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html GCC provides th ...

  4. 在golang中使用 cgo,如何让被嵌入的c语言代码调用golang

    https://golang.org/misc/cgo/test/callback.go // Copyright 2011 The Go Authors. All rights reserved. ...

  5. 高性能python

    参考来源:Python金融大数据分析第八章 提高性能有如下方法 1.Cython,用于合并python和c语言静态编译泛型 2.IPython.parallel,用于在本地或者集群上并行执行代码 3. ...

  6. Consuming JSON Strings in SQL Server

    https://www.simple-talk.com/sql/t-sql-programming/consuming-json-strings-in-sql-server/ Consuming JS ...

  7. clean code meaningful names

    ---恢复内容开始--- Meaningful Names: use Intention-Revealing Names //nice,Everyone who reads your code (in ...

  8. Working with Strings(使用Oracle字符串)

    Working with Strings By Steven Feuerstein  Part 3 in a series of articles on understanding and using ...

  9. Named function expressions demystified

    Introduction Surprisingly, a topic of named function expressions doesn't seem to be covered well eno ...

随机推荐

  1. 树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (五) 树莓派单子节点发送数据

    本项目中各个节点和树莓派的通信不区分信道,因此如果由树莓派发送给特定节点的数据会被所有节点接收到,因此子节点可以判别该数据是否发给自己的,需要在数据的第二个字节中加入目标节点的编号(第一个字节为源节点 ...

  2. Java Lock & Condition

    /* jdk1.5以后将同步和锁封装成了对象. 并将操作锁的隐式方式定义到了该对象中, 将隐式动作变成了显示动作. Lock接口: 出现替代了同步代码块或者同步函数.将同步的隐式锁操作变成现实锁操作. ...

  3. keil c51笔记

    第一章 Keil C51开发系统基本知识 第一节 系统概述 Keil C51是美国Keil Software公司出品的51系列兼容单片机C语言软件开发系统,与汇编相比,C语言在功能上.结构性.可读性. ...

  4. 【第十周】psp

    代码累计 300+575+475+353+620+703=2926 随笔字数 1700+3000+3785+4210+4333+3032=20727 知识点 机器学习,支持向量机 数据库技术 Acm刷 ...

  5. .NET Socket 简单入门

    说到Socket,想必大家都或多或少有所涉及,从最初的计算机网络课程,讲述了tcp协议,而Socket就是对协议的进一步封装,使我们开发人员能够更加容易轻松的进行软件之间的通信. 这个星期刚好接受一个 ...

  6. Linux下更改正确国内时间

    Linux使用小Tips 整理些Linux些常遇到的问题. Linux下设置时间 提供两种最根本有效的方式,就是更改时区.这里以更改为国内上海时间例子,其他地方时区同理. 方法一 备份文件 mv /e ...

  7. Thinkphp面试问题

    1.如何理解TP中的单一入口文件? 答:ThinkPHP采用单一入口模式进行项目部署和访问,无论完成什么功能,一个项目都有一个统一(但不一定是唯一)的入口.应该说,所有项目都是从入口文件开始的,并且所 ...

  8. Spring Boot快速搭建Spring框架

    Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development a ...

  9. 随机场(Random field)

    一.随机场定义 http://zh.wikipedia.org/zh-cn/随机场 随机场(Random field)定义如下: 在概率论中, 由样本空间Ω = {0, 1, …, G − 1}n取样 ...

  10. P2231 [HNOI2002]跳蚤

    题目描述 Z城市居住着很多只跳蚤.在Z城市周六生活频道有一个娱乐节目.一只跳蚤将被请上一个高空钢丝的正中央.钢丝很长,可以看作是无限长.节目主持人会给该跳蚤发一张卡片.卡片上写有N+1个自然数.其中最 ...