c – const,span和iterator麻烦
我尝试编写一个迭代器,通过索引迭代容器.
A It和const它都允许更改容器的内容. Const_it和const Const_it都禁止更改容器的内容. 之后,我尝试写一个span< T>在一个容器上. 代码无法编译,因为: // *this is const within a const method // But It<self_type> requires a non-const *this here. // So the code does not compile It<self_type> begin() const { return It<self_type>(*this,0); } 如果我使It的构造函数接受一个const容器,它看起来不正确,因为迭代器可以修改容器的内容. 如果我摆脱了方法的const,那么对于非const类型T,const span< T>无法修改容器. 它继承自Const_it,以允许在模板实例化期间从It到Const_it的隐式转换. 我在迭代器(const C * container_;)中使用指针而不是引用来允许将一个迭代器分配给另一个迭代器. 我怀疑这里有些不对劲,因为我甚至想到: Does cast away const of *this cause undefined behavior? 但我不知道如何解决它. 测试: #include <vector> #include <numeric> #include <iostream> template<typename C> class Const_it { typedef Const_it<C> self_type; public: Const_it(const C& container,const int ix) : container_(&container),ix_(ix) {} self_type& operator++() { ++ix_; return *this; } const int& operator*() const { return ref_a()[ix_]; } bool operator!=(const self_type& rhs) const { return ix_ != rhs.ix_; } protected: const C& ref_a() const { return *container_; } const C* container_; int ix_; }; template<typename C> class It : public Const_it<C> { typedef Const_it<C> Base; typedef It<C> self_type; public: //It(const C& container. It(C& container,const int ix) : Base::Const_it(container,ix) {} self_type& operator++() { ++ix_; return *this; } int& operator*() const { return mutable_a()[ix_]; } private: C& mutable_a() const { return const_cast<C&>(ref_a()); } using Base::ref_a; using Base::container_; using Base::ix_; }; template <typename V> class span { typedef span<V> self_type; public: explicit span(V& v) : v_(v) {} It<self_type> begin() { return It<self_type>(*this,0); } // *this is const within a const method // But It<self_type> requires a non-const *this here. // So the code does not compile It<self_type> begin() const { return It<self_type>(*this,0); } It<self_type> end() { return It<self_type>(*this,v_.size()); } It<self_type> end() const { return It<self_type>(*this,v_.size()); } int& operator[](const int ix) {return v_[ix];} const int& operator[](const int ix) const {return v_[ix];} private: V& v_; }; int main() { typedef std::vector<int> V; V v(10); std::iota(v.begin(),v.end(),0); std::cout << v.size() << "n"; const span<V> s(v); for (auto&& x : s) { x = 4; std::cout << x << "n"; } } 解决方法
有两个主要的注意事项要说这项工作.第一:
不是真的,因为模板中的C< typename C> class它不是实际的容器,而是跨度< V>.换句话说,看看: It<self_type> begin() const { return It<self_type>(*this,0); } 这里self_type表示const span< V>,因此您返回It< const span< V>>.因此,你的迭代器可以用const span做任何事情 – 但容器仍然是非const的.变量名称container_不是幸运的.
另外,既然你想让const span允许修改内容,那么你应该在span内部写的是(注意const): int& operator[](const int ix) const {return v_[ix];} // Removing the other `const` version: // const int& operator[](const int ix) const {return v_[ix];} 通过澄清这两个位,您可以构建一个工作示例.这是一个基于您的代码并简化以解决手头的问题: #include <vector> #include <iostream> template<typename S> class It { typedef It<S> self_type; const S& span_; int ix_; public: It(const S& span,const int ix) : span_(span),ix_(ix) {} self_type& operator++() { ++ix_; return *this; } int& operator*() const { return span_[ix_]; } bool operator!=(const self_type& rhs) const { return &span_ != &rhs.span_ or ix_ != rhs.ix_; } }; template <typename V> class span { typedef span<V> self_type; public: explicit span(V& v) : v_(v) {} It<self_type> begin() const { return It<self_type>(*this,0); } It<self_type> end() const { return It<self_type>(*this,v_.size()); } int& operator[](const int ix) const {return v_[ix];} private: V& v_; }; int main() { typedef std::vector<int> V; V v(10); const span<V> s(v); for (auto&& x : s) { x = 4; std::cout << x << "n"; } } 看一下operator!=的更正实现,以及不需要非const版本的begin()和end()这一事实.你也可以扔一个cbegin()和cend().然后你必须处理添加const迭代器的情况. 顺便说一句,如果它为任何人节省了一些混乱:在不久的将来,可能会增加 换句话说,作为模板参数,它将采用元素的类型,而不是容器: span<std::vector<int>> s(v); // vs std::span<int> s(v); 这允许std :: span的使用者避免知道幕后的容器(甚至没有容器:连续的内存区域或数组). 最后,您可能需要查看GSL’s implementation of (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |