#、##、#@在#define中的用法
来源:互联网 发布:工程软件管理 编辑:程序博客网 时间:2024/05/17 08:15
有如下C++代码:
- #include <iostream>
- using namespace std;
- #define A(exp) cout << "I am "#exp << endl;
- #define B(exp) cout << sz##exp << endl;
- #define C(exp) cout << #@exp << endl;
- int main()
- {
- char *szStr = "test";
- A(Chinese); // cout << "I am ""Chinese" << endl;
- B(Str); // cout << szStr << endl;
- C(a); // cout << 'a' << endl;
- C(ab); // cout << 'ab' << endl;
- return 0;
- }
运行结果如下:
展开的时候,#exp被扩展成字符串,##exp被扩展成子串,#@exp被扩展成字符。
注意:#@只实用于windows系统,MSDN如下说明
Operator
Action
Stringizing operator (#)
Causes the corresponding actual argument to be enclosed in double quotation marks
Charizing operator (#@)
Causes the corresponding argument to be enclosed in single quotation marks and to be treated as a character (Microsoft Specific)
Token-pasting operator (##)
Allows tokens used as actual arguments to be concatenated to form other tokens
可用作自定义ASSERT:
- #include <iostream>
- using namespace std;
- #ifdef ASSERT
- #undef ASSERT
- #endif
- #define ASSERT(exp)\
- if (!(##exp))\
- {\
- cout << "an error occured while execute \""#exp"\" at "\
- << __FILE__ << "(" << __LINE__ << ")" << endl;\
- exit(-1);\
- }
- int main()
- {
- int a = 0;
- int b = 1;
- ASSERT(a == b);
- cout << "test" << endl;
- return 0;
- }
http://blog.csdn.net/beanjoy/article/details/7577944
0 0
- #、##、#@在#define中的用法
- #、##、#@在#define中的用法
- “\”在#define语句中的用法
- 【转】#、##、#@在#define中的用法
- #define中的一种用法
- javascript中的define用法
- C中的define 特殊用法
- define在sv中的应用
- #(井号)在C/C++的#define宏定义中的特殊用法
- #(井号),##(双井号), #@在C/C++的#define宏定义中的特殊用法
- #(井号)在C/C++的#define宏定义中的特殊用法
- #(井号),##(双井号), #@在C/C++的#define宏定义中的特殊用法
- C语言中的#define用法总结
- c/c++中define中的用法
- #define用法
- define用法
- define用法
- #define用法
- 应届生应该如何面对工作?——以一个创业者的心态面对工作
- datagrid中添加合计行计算合计
- java-I/O流
- Java 使用 json-lib 处理 JSON
- 外链图2
- #、##、#@在#define中的用法
- 学习呈现的一些方法!
- 列出1~10000的所有质数。
- UIButton交互事件-点击button改变label文字
- 掌握Java的基本概念
- openstack安装注意事项
- 用原生js实现数字自相加
- Android Context 详解
- 在linux下命令执行sql(交互式)informix