#define中##用法

基本用法

  #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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值