宏定义中的#,##

来源:互联网 发布:cloudreader 大数据 编辑:程序博客网 时间:2024/06/13 07:11

1、在一个预处理器宏中的参数前面使用一个#,预处理器会把这个参数转换为一个字符数组。(原文:When you put a # before an argument in a preprocessor

macro, the preprocessor turns that argument into a character array. This,

combined with the fact that character arrays with no intervening punctuation are concatenated into a single character array, allows you to make a very convenient macro for printing the values of variables during debugging)

include “iostream”

using namespace std;

define P(A) cout<<#A<<”: “<<(A)<

define A1(name, type) type name_##type##_type 或

define A2(name, type) type name##_##type##_type

A1(a1, int); /* 等价于: int name_int_type; */

A2(a1, int); /* 等价于: int a1_int_type; */

解释:

1) 在第一个宏定义中,”name”和第一个”“之间,以及第2个”“和第二个”type”之间没有被分隔,所以预处理器会把name_##type##_type解释成3段:

“name_”、“type”、以及“_type”,这中间只有“type”是在宏前面出现过

的,所以它可以被宏替换。

2) 而在第二个宏定义中,“name”和第一个“_”之间也被分隔了,所以

预处理器会把name####type##_type解释成4段:“name”、“”、“type”

以及“_type”,这其间,就有两个可以被宏替换了。

3) A1和A2的定义也可以如下:

define A1(name, type) type name_ ##type ##_type

<##前面随意加上一些空格>

define A2(name, type) type name ##_ ##type ##_type

结果是## 会把前面的空格去掉完成强连接,得到和上面结果相同的宏定义

(3)其他相关 – 单独的一个 #

至于单独一个#,则表示对这个变量替换后,再加双引号引起来。比如

define __stringify_1(x) #x

那么

__stringify_1(Linux) <==> “linux”

(5)#(stringizing)字符串化操作符。其作用是:将宏定义中的传入参数名转换成用一对双引号括起来参数名字符串。其只能用于有传入参数的宏定义中,且必须置于宏定义体中的参数名前。

如:

define example(instr) printf(“the input string is:\t%s\n”,#instr)

define example1(instr) #instr

当使用该宏定义时:

example(abc); 在编译时将会展开成:printf(“the input string is:\t%s\n”,”abc”);

string str=example1(abc); 将会展成:string str=”abc”;

注意:

对空格的处理

a。忽略传入参数名前面和后面的空格。

如:str=example1( abc ); 将会被扩展成 str=”abc”;

b.当传入参数名间存在空格时,编译器将会自动连接各个子字符串,用每个子字符串中只以一个空格连接,忽略其中多于一个的空格。

如:str=exapme( abc def); 将会被扩展成 str=”abc def”;

其它参考

[1]http://blog.chinaunix.net/u/17855/showart_113663.html

原创粉丝点击