C++ 为boost::iterator_适配器设置迭代器_括号_代理

C++ 为boost::iterator_适配器设置迭代器_括号_代理,c++,boost,iterator,C++,Boost,Iterator,我试着做一个随机访问迭代器 它的索引操作应该返回一个节点 但事实并非如此。如何让它做到这一点 测试: 错误: error: ‘boost::iterators::detail ::operator_brackets_result<It<int>, Node<int>, Node<int> >::type {aka class boost::iterators::detail ::operator_brackets_proxy<

我试着做一个随机访问迭代器

它的索引操作应该返回一个节点

但事实并非如此。如何让它做到这一点

测试:

错误:

error: ‘boost::iterators::detail
    ::operator_brackets_result<It<int>, Node<int>, Node<int> >::type 
{aka class boost::iterators::detail
    ::operator_brackets_proxy<It<int> >}’ 
has no member named ‘index_’
     std::cout << a[4].index_ << "\n";
                       ^~~~~~
见文件

使用iterator_facade构建的可写迭代器实现首选解决方案所需的语义,并由提案采用: 1 p[n]的结果是一个可转换为迭代器值_类型的对象,p[n]=x相当于*p+n=x注意:此结果对象可以作为包含p+n副本的代理来实现。这种方法将适用于任何随机访问迭代器,而不考虑其实现的其他细节。 2A了解迭代器实现更多信息的用户可以自由实现运算符[],该运算符在派生迭代器类中返回左值;它将对迭代器的客户机隐藏迭代器提供的外观

从1开始,您可以写:

std::cout << static_cast<Node<int>>(a[4]).index_ << "\n";
或者写:

std::cout << std::next(a,4)->index_ << "\n";
std::cout << (a+4)->index_ << "\n";
Node<T> operator[](std::ptrdiff_t n) const {
    return *std::next(*this,n);
}
std::cout << a[4].index_ << "\n";
std::cout << std::next(a,4)->index_ << "\n";
std::cout << (a+4)->index_ << "\n";