BOOST_FOREACH是不是搞过头了

看看BOOST_FOREACH的实现吧,你真的会为了少敲几个字母,为了代码界面上的简单而使用BOOST_FOREACH吗?

///
// BOOST_FOREACH
//
//   For iterating over collections. Collections can be
//   arrays, null-terminated strings, or STL containers.
//   The loop variable can be a value or reference. For
//   example:
//
//   std::list<int> int_list(/*stuff*/);
//   BOOST_FOREACH(int &i, int_list)
//   {
//       /*
//        * loop body goes here.
//        * i is a reference to the int in int_list.
//        */
//   }
//
//   Alternately, you can declare the loop variable first,
//   so you can access it after the loop finishes. Obviously,
//   if you do it this way, then the loop variable cannot be
//   a reference.
//
//   int i;
//   BOOST_FOREACH(i, int_list)
//       { ... }
//
#define BOOST_FOREACH(VAR, COL)                                                                 /
    BOOST_FOREACH_PREAMBLE()                                                                    /
    if (boost::foreach_detail_::auto_any_t _foreach_col = BOOST_FOREACH_CONTAIN(COL)) {} else   /
    if (boost::foreach_detail_::auto_any_t _foreach_cur = BOOST_FOREACH_BEGIN(COL)) {} else     /
    if (boost::foreach_detail_::auto_any_t _foreach_end = BOOST_FOREACH_END(COL)) {} else       /
    for (bool _foreach_continue = true;                                                         /
              _foreach_continue && !BOOST_FOREACH_DONE(COL);                                    /
              _foreach_continue ? BOOST_FOREACH_NEXT(COL) : (void)0)                            /
        if  (boost::foreach_detail_::set_false(_foreach_continue)) {} else                      /
        for (VAR = BOOST_FOREACH_DEREF(COL); !_foreach_continue; _foreach_continue = true)

#endif

//代码一共有800多行,我列出了最后的注释和定义。

我觉得BOOST_FOREACH有点搞过头了,手写for的循环,最多也就两行,为什么要为形式上的简单而引入如此多的定义和编译器解析。而且这还是个宏。我不是反对宏,只是觉得宏在这个地方没带来太多好处,反而添乱,调试的噩梦,郁闷死。

不是每个库都是那么精彩和实用的。std::vector<bool>的特化,std::auto_ptr的设计也都不那么让人舒适。保持脑袋清醒,有选择的使用类库吧。

最后,还是要感叹一下BOOST_FOREACH实现,太牛了。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值