const,span和迭代器的麻烦

时间:2018-05-03 22:19:17

标签: c++ c++11 const

我尝试编写一个迭代器,它通过索引遍历容器。 Itconst It都允许更改容器的内容。 Const_itconst Const_it都禁止更改容器的内容。

之后,我尝试在容器上写span<T>。 对于非常量类型Tconst span<T>span<T>都允许更改容器的内容。 const span<const T>span<const 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>不能修改容器。

It继承自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";
    }
}

2 个答案:

答案 0 :(得分:1)

要完成这项工作,有两个主要注意事项。第一:

  

如果我使It的构造函数接受一个const容器,它看起来不正确,因为迭代器可以修改容器的内容。

不是真的,因为C中的template<typename C> class It 不是实际容器,而是span<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_并不幸运。

  

对于非T的{​​{1}}类型,constconst span<T>都允许更改容器的内容。 span<T>const span<const T>都禁止更改容器的内容。

此外,既然您希望允许span<const T>修改内容,那么您应该在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!=constbegin()这一事实。你也可以扔end()cbegin()。然后你必须努力添加const迭代器的情况。

顺便说一句,如果它为任何人节省了一些混淆:在不久的将来,可能会添加std::spanproposed for C++20);它只是一个cend()对 - 而不是您的(pointer-to-first-element, index)版本。

换句话说,作为模板参数,它将采用元素的类型,而不是容器:

(pointer-to-container, index)

这允许span<std::vector<int>> s(v); // vs std::span<int> s(v); 的消费者避免知道幕后的容器(甚至没有容器:连续的内存区域或数组)。

最后,您可能需要查看GSL's implementation of std::span以获得有关如何完全实现它的一些灵感(包括关于范围的第二个模板参数)。

答案 1 :(得分:0)

在研究了Acorn的解决方案之后,我找到了另一个解决方案。

这允许对std::vector<T>span使用相同的迭代器模板。

这两种情况不同。 对于非const类型Tconst std::vector<T>禁止更改其元素。 const span<T>允许更改其元素。

主要区别在于It<const self_type>(*this, 0);班级spanS& span_;班级const S& span_;而不是It

修正:

#include <vector>
#include <iostream>

template<typename S>
class It {
    typedef It<S> self_type;
public:
    It(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_;
    }

private:
    S& span_;
    int ix_;
};

template<typename V>
class span {
    typedef span<V> self_type;
public:
    explicit span(V& v) : v_(v) {}

    It<const self_type> begin() const {
        return It<const self_type>(*this, 0);
    }

    It<const self_type> end() const {
        return It<const self_type>(*this, v_.size());
    }

    int& operator[](const int ix) const { return v_[ix]; }

private:
    V& v_;
};

int main() {
    // Test adding iterator to a span
    typedef std::vector<int> V;
    V v(10);
    const span<V> s(v);
    for (auto&& x : s) {
        x = 4;
        std::cout << x << " ";
    }
    std::cout << "\n";
    // Test adding iterator to a std::vector
    const It<V> begin(v, 0);
    const It<V> end(v, v.size());

    for (auto it = begin; it != end; ++it) {
        *it = 10;
        std::cout << *it << " ";
    }

    std::cout << "\n";
}