Token-Pasting Operator (##) and Stringizing Operator (#)
Token-Pasting Operator (##)
The double-number-sign or “token-pasting” operator (##), which is sometimes called the “merging” operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token and therefore cannot be the first or last token in the macro definition.
If a formal parameter in a macro definition is preceded or followed by the token-pasting operator, the formal parameter is immediately replaced by the unexpanded actual argument. Macro expansion is not performed on the argument prior to replacement.
Then, each occurrence of the token-pasting operator in token-string is removed, and the tokens preceding and following it are concatenated. The resulting token must be a valid token. If it is, the token is scanned for possible replacement if it represents a macro name. The identifier represents the name by which the concatenated tokens will be known in the program before replacement. Each token represents a token defined elsewhere, either within the program or on the compiler command line. White space preceding or following the operator is optional.
This example illustrates use of both the stringizing and token-pasting operators in specifying program output:
#define paster( n ) printf( "token" #n " = %d", token##n )
int token9 = 9;
If a macro is called with a numeric argument like
paster( 9 );
the macro yields
printf( "token" "9" " = %d", token9 );
which becomes
printf( "token9 = %d", token9 );
Stringizing Operator (#)
The number-sign or “stringizing” operator (#) converts macro parameters (after expansion) to string constants. It is used only with macros that take arguments. If it precedes a formal parameter in the macro definition, the actual argument passed by the macro invocation is enclosed in quotation marks and treated as a string literal. The string literal then replaces each occurrence of a combination of the stringizing operator and formal parameter within the macro definition.
White space preceding the first token of the actual argument and following the last token of the actual argument is ignored. Any white space between the tokens in the actual argument is reduced to a single white space in the resulting string literal. Thus, if a comment occurs between two tokens in the actual argument, it is reduced to a single white space. The resulting string literal is automatically concatenated with any adjacent string literals from which it is separated only by white space.
Further, if a character contained in the argument usually requires an escape sequence when used in a string literal (for example, the quotation mark (") or backslash (\) character), the necessary escape backslash is automatically inserted before the character. The following example shows a macro definition that includes the stringizing operator and a main function that invokes the macro:
#define stringer( x ) printf( #x "\n" )
void main()
{
stringer( In quotes in the printf function call\n );
stringer( "In quotes when printed to the screen"\n );
stringer( "This: \" prints an escaped double quote" );
}
Such invocations would be expanded during preprocessing, producing the following code:
void main()
{
printf( "In quotes in the printf function call\n" "\n" );
printf( "\"In quotes when printed to the screen\"\n" "\n" );
printf( "\"This: \\\" prints an escaped double quote\"" "\n" );
}
When the program is run, screen output for each line is as follows:
In quotes in the printf function call
"In quotes when printed to the screen"
"This: \" prints an escaped double quotation mark"
Token-Pasting Operator (##) and Stringizing Operator (#)的更多相关文章
- c/c++的预处理定义 Stringizing Operator (#) Charizing Operator (#@) Token-Pasting Operator (##)
c/c++的预处理定义:一.Stringizing Operator (#)在c和c++中数字标志符#被赋予了新的意义,即字符串化操作符.其作用是:将宏定义中的传入参数名转换成用一对双引号括起来参数名 ...
- Effective C++ 第二版 8) 写operator new 和operator delete 9) 避免隐藏标准形式的new
条款8 写operator new 和operator delete 时要遵循常规 重写operator new时, 函数提供的行为要和系统缺省的operator new一致: 1)正确的返回值; 2 ...
- 从零开始学C++之重载 operator new 和 operator delete 实现一个简单内存泄漏跟踪器
先来说下实现思路:可以实现一个Trace类,调用 operator new 的时候就将指向分配内存的指针.当前文件.当前行等信息添加进Trace 成员map容器内,在调用operator delete ...
- operator new和operator delete
从STL源码剖析中看到了operator new的使用 template<class T> inline void _deallocate(T* buffer) { ::operator ...
- 定制自己的new和delete:operator new 和 operator delete
new和delete不同用法 基本用法 int * aptr = new int(10); delete aptr, aptr = nullptr; 上面的代码是我们最基本也是最常见的使用new和de ...
- C++面向对象高级编程(九)Reference与重载operator new和operator delete
摘要: 技术在于交流.沟通,转载请注明出处并保持作品的完整性. 一 Reference 引用:之前提及过,他的主要作用就是取别名,与指针很相似,实现也是基于指针. 1.引用必须有初值,且不能引用nul ...
- operator new 和 operator delete 实现一个简单内存泄漏跟踪器
先来说下实现思路:可以实现一个Trace类,调用 operator new 的时候就将指向分配内存的指针.当前文件.当前行等信息添加进Trace 成员map容器内,在调用operator delete ...
- 类型转换运算符、*运算符重载、->运算符重载、operator new 和 operator delete
一.类型转换运算符 必须是成员函数,不能是友元函数 没有参数 不能指定返回类型 函数原型:operator 类型名(); C++ Code 1 2 3 4 5 6 7 8 9 10 11 12 1 ...
- ZT 自定义operator new与operator delete的使用(1)
http://blog.csdn.net/waken_ma/article/details/4004972 先转两篇文章: 拨开自定义operator new与operator delete的迷雾 C ...
随机推荐
- humble_USACO
Humble Numbers For a given set of K prime numbers S = {p1, p2, ..., pK}, consider the set of all num ...
- win10出现"win10系统即将过期,请激活"的处理办法
当打开电脑时,出现"你的win10系统即将过期,请前往激活”的提示.上网查了解决方案,避免以后在出现这样的情况,现将解决步骤整理如下: 1.下载KMSpico激活软件,百度网盘下载链接如下: ...
- 解决springmvc+fastjson返回页面出现乱码问题
在controller里面的接口上面加,produces="text/html;charset=UTF-8"即可 @RequestMapping(value = "/ad ...
- JVM核心机制(类加载器、三种类加载器、代理加载模式、双亲委派机制
- linux命令(30):touch命令
实例一:创建不存在的文件 touch test.log test1.log 实例二:更新log.log的时间和log2012.log时间戳相同 touch -r test.log test1.log ...
- P1474 货币系统 Money Systems(完全背包求填充方案数)
题目链接:https://www.luogu.org/problemnew/show/1474 题目大意:有V种货币,求用V种货币凑出面值N有多少种方案. 解题思路:就是完全背包问题,只是将求最大价值 ...
- js解析与序列化json数据(一)json.stringify()的基本用法
对象有两个方法:stringify()和parse().在最简单的情况下,这两个方法分别用于把JavaScript对象序列化为JSON字符串和把JSON字符串解析为原生JavaScript 早期的JS ...
- RSA的公钥和私钥到底哪个才是用来加密和哪个用来解密?
https://www.zhihu.com/question/25912483?sort=created
- 解决Django在mariadb创建的表插入中文乱码的问题
1.确保你的mariadb数据库的character_set_connection.character_set_database.character_set_server的编码均为utf8 Maria ...
- OpenStack 认证服务 KeyStone连接和用户管理(五)
一) 创建环境变量链接keyston vim adminrc export OS_USERNAME=admin export OS_PASSWORD=redhat export OS_PROJECT_ ...