Maxbad`Blog

c++ 宏

2020-10-11 · 3 min read

https://github.com/Light-City/CPlusPlusThings/tree/master/basic_content/macro

字符串化操作符“#”

如果熟悉 C++ 宏的小伙伴,可能知道里面有个 # 的用法,可以将对应的参数变成字符串,
注意:其只能用于有传入参数的宏定义中,且必须置于宏定义体中的参数名前。
效果如下:

#define exp(s) printf("test s is:%s\n",s)
#define exp1(s) printf("test s is:%s\n",#s)
#define exp2(s) #s 
int main() {
    exp("hello");
    exp1(hello);

    string str = exp2(   bac );
    cout<<str<<" "<<str.size()<<endl;
    /**
     * 忽略传入参数名前面和后面的空格。
     */
    string str1 = exp2( asda  bac );
    /**
     * 当传入参数名间存在空格时,编译器将会自动连接各个子字符串,
     * 用每个子字符串之间以一个空格连接,忽略剩余空格。
     */
    cout<<str1<<" "<<str1.size()<<endl;
    return 0;
}

那么对于单个变量最简单的宏实现方式就是:

#define logs(x)  std::cout << #x":" << x << std::endl;
......
int delay = 5;
int other = 3;
logs(delay);
logs(other);

// 输出结果
// delay:5
// other:3

符号连接操作符 ,将前后两个的单词拼接在一起“##”

“##”是一种分隔连接方式,它的作用是先分隔,然后进行强制连接。将宏定义的多个形参转换成一个实际参数名。
注意事项:
(1)当用##连接形参时,##前后的空格可有可无。
(2)连接后的实际参数名,必须为实际存在的参数名或是编译器已知的宏定义。
(3)如果##后的参数本身也是一个宏的话,##会阻止这个宏的展开。

#define expA(s) printf("前缀加上后的字符串为:%s\n",gc_##s)  //gc_s必须存在
// 注意事项2
#define expB(s) printf("前缀加上后的字符串为:%s\n",gc_  ##  s)  //gc_s必须存在
// 注意事项1
#define gc_hello1 "I am gc_hello1"
int main() {
    // 注意事项1
    const char * gc_hello = "I am gc_hello";
    expA(hello);
    expB(hello1);
}

将值序列变为一个字符 "#@"

#define ch(c) #@c
 //  调用
 ch(a)
 // 展开后成为:'a'