#define宏定义中的#和##用法和区别

参考Difference between # and ##
简单来说,在宏定义中,#是将变量转换成字符串的作用,而##是连接两个参数作用

###exaplem

#include 

#define stringize_indirect( a ) stringize(a)
#define stringize( a ) #a
#define paste( a, b ) a ## b

int main()
{
    std::cout << stringize( strin ) stringize( gize ) << '\n' ;
    // std::cout << "strin" "gize" << '\n' ;

    std::cout << paste(1,2) << '\n';

    std::cout << stringize( paste( strin, gize ) ) << '\n' ;
    // std::cout << "paste( strin, gize )" << '\n' ;

    std::cout << stringize_indirect( paste( strin, gize ) ) << '\n' ;
    // std::cout << "stringize" << '\n' ;
}

运行结果

stringize
12
paste( strin, gize )
stringize

你可能感兴趣的:(C++编程)