#define中##用法
2017-07-17 09:36
232 查看
基本用法
#define to_string( s ) #s
cout < < to_string( Hello World! ) < < endl;
理解为 cout < < "Hello World!" < < endl;
使用##连结##前后的内容.
#define concatenate( x, y ) x##y
int xy = 10;
cout < < concatenate( x, y ) < < endl;
解释为 cout < < xy < < endl;
理所当然,将会在标准输出处显示'10'.
#define f(a,b) a##b
#define d(a) #a --》 以"#"开头的,直接替换,不展开:immediately replaced by the unexpanded actual argument
#define s(a) d(a) --》 非以"#"开头的,先展开,再替换,也就是一般的情况
void main( void )
{
puts(d(f(a,b)));
puts(s(f(a,b)));
}
输出结果:
f(a,b)
ab
所以就两种情况:
1,不以"#"开头的,先展开参数a,然后是替换代码:puts(s(f(a,b)));-->puts(s(ab))-->puts(d(ab))-->puts("ab")
2,以"#"开头的,直接替换,不展开:puts(d(f(a,b)))-->puts("f(a,b)")
总结:先看外层 如果第一个宏是#开头则直接展开即可 否则从内向外展开
看个Traffic Server中Continuation协程类的定义
其实很简单 只要不粗心 真的用脑思考就会发现 协程类中有个link成员
而link中包含了 前驱节点指针和后续节点指针 即协程类是个双向的链表
通过link成员 将各节点串联起来
通过Link_link成员 返回出前驱、后续节点
参考链接 http://blog.csdn.net/qq_15457239/article/details/56842450
#define to_string( s ) #s
cout < < to_string( Hello World! ) < < endl;
理解为 cout < < "Hello World!" < < endl;
使用##连结##前后的内容.
#define concatenate( x, y ) x##y
int xy = 10;
cout < < concatenate( x, y ) < < endl;
解释为 cout < < xy < < endl;
理所当然,将会在标准输出处显示'10'.
#define f(a,b) a##b
#define d(a) #a --》 以"#"开头的,直接替换,不展开:immediately replaced by the unexpanded actual argument
#define s(a) d(a) --》 非以"#"开头的,先展开,再替换,也就是一般的情况
void main( void )
{
puts(d(f(a,b)));
puts(s(f(a,b)));
}
输出结果:
f(a,b)
ab
所以就两种情况:
1,不以"#"开头的,先展开参数a,然后是替换代码:puts(s(f(a,b)));-->puts(s(ab))-->puts(d(ab))-->puts("ab")
2,以"#"开头的,直接替换,不展开:puts(d(f(a,b)))-->puts("f(a,b)")
总结:先看外层 如果第一个宏是#开头则直接展开即可 否则从内向外展开
看个Traffic Server中Continuation协程类的定义
#include <iostream> class Continuation; template <class C> class SLink { public: C *next; SLink() : next(NULL){}; }; template <class C> struct Link : public SLink<C> { C *prev; Link() : prev(NULL) {} }; #define LINK(_c, _f) \ class Link##_##_f : public Link<_c> \ { \ public: \ static _c *& \ next_link(_c *c) \ { \ return c->_f.next; \ } \ static _c *& \ prev_link(_c *c) \ { \ return c->_f.prev; \ } \ static const _c * \ next_link(const _c *c) \ { \ return c->_f.next; \ } \ static const _c * \ prev_link(const _c *c) \ { \ return c->_f.prev; \ } \ }; \ Link<_c> _f class Continuation { public: LINK(Continuation, link); void print() {std::cout<<"fun\n";} }; int main() { Continuation c1; Continuation c2; c1.link.next = &c2; c2.link.prev = &c1; return 0; }
其实很简单 只要不粗心 真的用脑思考就会发现 协程类中有个link成员
而link中包含了 前驱节点指针和后续节点指针 即协程类是个双向的链表
通过link成员 将各节点串联起来
通过Link_link成员 返回出前驱、后续节点
参考链接 http://blog.csdn.net/qq_15457239/article/details/56842450
相关文章推荐
- #define中 #与##的神奇用法
- #define的特别用法 ,##和#@还有# 的意思
- #define中 #与##的神奇用法linux学习
- #define中 #与##的神奇用法
- #define中 #与##的神奇用法
- #define中 #与##的神奇用法
- #define中 #与##的神奇用法
- #define中 #与##的神奇用法linux学习 (转)
- #define中 #与##的神奇用法
- #define中 #与##的神奇用法
- #define中 #与##的神奇用法linux学习 (转)
- #define中 #与##的用法
- #define中 #与##的用法
- #define中 #与##的神奇用法
- #define中 #与##的神奇用法
- #define中 #与##的神奇用法
- #define 高级用法(Ex) __FILE__ __FUNCTION__ __LINE__
- typedef和#define的用法与区别----转载
- typedef和#define的用法与区别
- typedef和#define的用法与区别