- #
把宏参数变为一个字符串,仅可用于宏定义中。
如:
int GetLength(std::string str)
{
return str.length();
}
#define LEN(str) GetLength(#str)
int main()
{
std::string str = "abcdefg";
cout << LEN(str) << endl;
return 0;
}
- ##
把两个宏参数连接到一起,仅可用于宏定义中。
如:
int GetLength(std::string str)
{
return str.length();
}
#define TOTALLEN(str1, str2) GetLength(str1##str2)
int main()
{
std::string str1 = "abcdefg";
std::string str2 = "hijk";
std::string str1str2 = "hello";
cout << TOTALLEN(str1,str2) << endl;
return 0;
}
- \
在宏定义中用于将不同行的上下文连接成一行 - __DATE__
在源文件中插入当前编译的日期 - __TIME__
在源文件中插入当前编译的时间 - __FILE__
在源文件中插入当前源文件路径及文件名 - __LINE__
在源文件中插入当前源代码的行号