自定义迭代器不适用于BOOST_FOREACH?

自定义迭代器不适用于BOOST_FOREACH?,boost,iterator,Boost,Iterator,我有一个包含一些数据的类,我想添加begin()和end()函数,它们为数据的ID提供迭代器 我正在使用Boost计数迭代器: #include <iostream> #include <vector> #include <boost/foreach.hpp> #include <boost/iterator/counting_iterator.hpp> template<class T> class ContainerTpl

我有一个包含一些数据的类,我想添加
begin()
end()
函数,它们为数据的ID提供迭代器

我正在使用Boost
计数迭代器

#include <iostream>
#include <vector>
#include <boost/foreach.hpp>
#include <boost/iterator/counting_iterator.hpp>

template<class T>   
class ContainerTpl {
 public:
  typedef std::size_t Id;
  typedef boost::counting_iterator<Id> const_iterator;
  ContainerTpl() {}
  const_iterator begin() {
    return boost::counting_iterator<Id>(0);
  }
  const_iterator end() {
    return boost::counting_iterator<Id>(container_.size());
  }
 private:
  std::vector<T> container_;
};

int main () {
  typedef ContainerTpl<double> Container;
  Container c;
  BOOST_FOREACH (Container::Id cid, c) {
    std::cerr << cid << std::endl;
  }
  return 0;
}
我怎样才能使代码工作

更新:在回答之后,我添加了以下代码,使其能够工作:

namespace boost
{
// specialize range_mutable_iterator and range_const_iterator in                                                                                                                                    
// namespace boost                                                                                                                                                                                  
template<class T>
struct range_mutable_iterator< ContainerTpl<T> > {
  typedef typename ContainerTpl<T>::const_iterator type;
};

template<class T>
struct range_const_iterator< ContainerTpl<T> > {
  typedef typename ContainerTpl<T>::const_iterator type;
};
} // end namespace         
namespace boost
{
//专门化中的范围可变迭代器和范围常数迭代器
//名称空间提升
模板
结构范围可变迭代器{
typedef typename ContainerTpl::const_迭代器类型;
};
模板
结构范围常量迭代器{
typedef typename ContainerTpl::const_迭代器类型;
};
}//结束命名空间

Boost文档中有一页是关于这一点的:

简而言之,您需要为您的类型定义
boost::range\u mutable\u iterator
,以便编译器可以实例化
boost\u FOREACH
尝试使用的模板类型

为未来的谷歌编辑:

我不确定那个“沙盒”URL是否总是指向最新版本,或者是一个最终会崩溃的临时位置。这是当前版本的链接,该版本可能更稳定,但会过时:


我知道这是一个老问题,但似乎仍然相关。我遇到了这个问题,并注意到boost foreach需要定义一个可变迭代器和一个常量迭代器(因此,ContainerTpl::iterator和ContainerTpl::const_迭代器)。否则,您需要按照Tim提供的说明进行操作。

这对操作至关重要。在我的例子中,
typedef迭代器const\u迭代器
已经足够了,但它必须存在。否则,您将获得神秘的编译器错误:/
namespace boost
{
// specialize range_mutable_iterator and range_const_iterator in                                                                                                                                    
// namespace boost                                                                                                                                                                                  
template<class T>
struct range_mutable_iterator< ContainerTpl<T> > {
  typedef typename ContainerTpl<T>::const_iterator type;
};

template<class T>
struct range_const_iterator< ContainerTpl<T> > {
  typedef typename ContainerTpl<T>::const_iterator type;
};
} // end namespace