4
我使用沉重的BOOST_FOREACH
來迭代容器,並且由於我最近搬到了C++ 0x,我以爲我可以用基於範圍的for
構造替代BOOST_FOREACH
。下面的代碼基於範圍的STL容器
#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> > >]
是否有辦法避免這種含糊不清,或者是基於範圍在這種情況下根本無法使用?
哇,謝謝!我嘗試了一個極小的exmaple,並且只在#include時纔會中斷。我將編輯問題以顯示它。 –
eudoxos
Hm ...最好是有更通用的東西,因爲每個容器類型的迭代器(begin,const begin,end,const end)都必須用這種方式包裝。希望'boost :: serialization'將支持'std :: shared_ptr'不久,我就可以放棄'boost :: shared_ptr': - | – eudoxos
@eudoxos:此問題[將被修復](http://boost.2283326.n4.nabble.com/Range-begin-end-ADL-issues-in-C-0x-range-based-for-td3093206。 html)在未來的Boost發佈。 –