【转】#、##、#@在#define中的用法
来源:互联网 发布:端口查询 编辑:程序博客网 时间:2024/06/05 16:44
转自 http://blog.csdn.net/beanjoy/article/details/7577944
有如下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;
- }
0 0
- 【转】#、##、#@在#define中的用法
- #、##、#@在#define中的用法
- #、##、#@在#define中的用法
- “\”在#define语句中的用法
- #define中的一种用法
- javascript中的define用法
- C中的define 特殊用法
- #define用法【转】
- define在sv中的应用
- #(井号)在C/C++的#define宏定义中的特殊用法
- #(井号),##(双井号), #@在C/C++的#define宏定义中的特殊用法
- #(井号)在C/C++的#define宏定义中的特殊用法
- #(井号),##(双井号), #@在C/C++的#define宏定义中的特殊用法
- C语言中的#define用法总结
- c/c++中define中的用法
- #define的一些用法(转)
- [转]C/C++ define 用法
- #define用法
- STL transform() demo
- HDU 2095--Find your present (2) 【set】
- ps for mac 破解版安装
- *LeetCode-Binary Tree Level Order Traversal
- DB_BLOCK_CHECKSUM&&DB_BLOCK_CHECKING
- 【转】#、##、#@在#define中的用法
- 商品销售系统
- 利用opencv的hog+svm实现细胞识别分类器
- Zookeeper:服务器和会话
- 2.Add Two Numbers
- java 解惑3 string 字符串
- java读取配置文件
- 从今天起,写一本关于LLVM的书----《深入理解LLVM》
- 3.Longest Substring Without Repeating Characters