加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

基于范围的STL容器

发布时间:2020-12-16 07:35:46 所属栏目:百科 来源:网络整理
导读:我使用大量的BOOST_FOREACH来迭代容器,因为我最近移动到了c 0x,我想我可以用基于范围的构造代替BOOST_FOREACH.下面这段代码 #includevector#includeboost/shared_ptr.hpp#includeboost/range.hppusing std::vector; using boost::shared_ptr;class Node;int
我使用大量的BOOST_FOREACH来迭代容器,因为我最近移动到了c 0x,我想我可以用基于范围的构造代替BOOST_FOREACH.下面这段代码

#include<vector>
#include<boost/shared_ptr.hpp>
#include<boost/range.hpp>
using std::vector; using boost::shared_ptr;
class Node;
int main(void){
   vector<shared_ptr<Node>> nodes;
   for(const shared_ptr<Node>& n: nodes);
}

不能用gcc 4.6编译,导致

error: call of overloaded 'end(std::vector<boost::shared_ptr<Node> >&)' is ambiguous
note: candidates are:
/usr/include/c++/4.6/bits/range_access.h:78:5: note: decltype (__cont->end()) std::end(const _Container&) [with _Container = std::vector<boost::shared_ptr<Node> >,decltype (__cont->end()) = __gnu_cxx::__normal_iterator<const boost::shared_ptr<Node>*,std::vector<boost::shared_ptr<Node> > >]
/usr/include/c++/4.6/bits/range_access.h:68:5: note: decltype (__cont->end()) std::end(_Container&) [with _Container = std::vector<boost::shared_ptr<Node> >,decltype (__cont->end()) = __gnu_cxx::__normal_iterator<boost::shared_ptr<Node>*,std::vector<boost::shared_ptr<Node> > >]
/usr/include/boost/range/end.hpp:103:47: note: typename boost::range_iterator<const T>::type boost::end(const T&) [with T = std::vector<boost::shared_ptr<Node> >,typename boost::range_iterator<const T>::type = __gnu_cxx::__normal_iterator<const boost::shared_ptr<Node>*,std::vector<boost::shared_ptr<Node> > >]
/usr/include/boost/range/end.hpp:92:41: note: typename boost::range_iterator<C>::type boost::end(T&) [with T = std::vector<boost::shared_ptr<Node> >,typename boost::range_iterator<C>::type = __gnu_cxx::__normal_iterator<boost::shared_ptr<Node>*,std::vector<boost::shared_ptr<Node> > >]

有没有办法避免这种模糊,或者是基于范围的,在这种情况下根本无法使用?

解决方法

棘手.你正在引入std :: end和boost :: end,因为std :: vector< boost :: shared_ptr>的关联命名空间都是std和boost.两者都是匹配的模板.

但是,非模板end()将是更好的匹配.所以,只提供你自己的:

inline std::vector<boost::shared_ptr<Node> >::iterator
   end(std::vector<boost::shared_ptr<Node> > vsn&)
{
  return std::end(vsn); 
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读