宏定义中的#和##

来源:互联网 发布:淘宝全国手机缴费 编辑:程序博客网 时间:2024/04/25 17:41

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)<<endl;

int main()

{

int a=1,b=2;

P(a);

P(b);

P(a+b);

return 1;

}

http://blog.163.com/zhoumhan_0351/blog/static/39954227201032124942513/


把变量和表达式转换成字符串

   在一个预处理器宏中的参数前面使用一个#,预处理器会把这个参数转换为一个字符数组。(原文: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)<<endl;

int main()

{

int a=1,b=2;

P(a);

P(b);

P(a+b);

return 1;

}

下图中下面的那个结果窗口,可以看到,#A输出了变量名,而A仅仅是输出了这是值。

42、Thinking in C++前几章笔记(一) - EdwardLewis - 墨涵天地


2#define D(a) cout << #a "=[" << a << "]" << endl;

3、#字符串化的意思。出现在宏定义中的#是把跟在后面的参数转换成一个字符串

例如:
            > #define  FOO(arg)   my##arg
        则
            > FOO(abc)
        相当于   myabc

例如:
            > #define STRCPY(dst, src)   strcpy(dst, #src)
        则
            > STRCPY(buff, abc)
        相当于   strcpy(buff, "abc")

另外,如果##后的参数本身也是一个宏的话,##会阻止这个宏的展开,也就是只替换一次。

    #define STRCPY(a, b)    strcpy(a ## _p, #b)

    int main()

    {

        char var1_p[20];

        char var2_p[30];

         /* 注意这里 */

        STRCPY(STRCPY(var1,var2),var2);

        /* 这里是否会展开为: strcpy(strcpy(var1_p,"var2")_p,"var2“)?

         * 答案是否定的:

         * 展开结果将是:  strcpy(STRCPY(var1,var2)_p,"var2")

         * ## 阻止了参数的宏展开!

         * 如果宏定义里没有用到 和 ##, 宏将会完全展开

         */

    }  

http://blog.chinaunix.net/u/17855/showart_113663.html

4about ## in common text

1)关于记号粘贴操作符(token paste operator): ##

简单的说,“##”是一种分隔连接方式,它的作用是先分隔,然后进行强制连接。

其中,分隔的作用类似于空格。我们知道在普通的宏定义中,预处理器一般把空格解释成分段标志,对于每一段和前面比较,相同的就被替换。但是这样做的结果是,被替换段之间存在一些空格。如果我们不希望出现这些空格,就可以通过添加一些##来替代空格。

另外一些分隔标志是,包括操作符,比如 +, -, *, /, [,], ...,所以尽管下面的

宏定义没有空格,但是依然表达有意义的定义: define add(a, b)  a+b

而其强制连接的作用是,去掉和前面的字符串之间的空格,而把两者连接起来。

2)举列 -- 试比较下述几个宏定义的区别

#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) A1A2的定义也可以如下:

#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"

其它参考

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


原创粉丝点击