const、span 和迭代器问题

R z*_* zu 6 c++ constants c++11

我尝试编写一个迭代器,通过索引迭代容器。AIt和 aconst It都允许更改容器的内容。AConst_it和 aconst Const_it都禁止更改容器的内容。

之后,我尝试span<T>在容器上写一个。对于一个类型T不是常量,这两个const 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); }
Run Code Online (Sandbox Code Playgroud)

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

如果我去掉方法的const,那么对于非const 类型T,aconst span<T>不能修改容器。

It从继承Const_it到允许隐式转换ItConst_it模板实例化时。

我在迭代器 ( const C* container_;) 中使用指针而不是引用来允许将一个迭代器分配给另一个迭代器。

我怀疑这里有什么地方不对劲,因为我什至在想:

抛弃 *this 的 const 会导致未定义的行为吗?

但我不知道如何修复它。

测试:

#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";
    }
}
Run Code Online (Sandbox Code Playgroud)

Aco*_*orn 2

要使这项工作顺利进行,有两个主要注意事项。第一的:

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

不是真的,因为C您的template<typename C> class It不是实际容器,而是span<V>. 换句话说,看看:

It<self_type> begin() const { return It<self_type>(*this, 0); }
Run Code Online (Sandbox Code Playgroud)

这里的self_type意思是const span<V>,因此你要返回 a It<const span<V>>。因此,你的迭代器可以做任何可以用 -- 完成的事情const span,但容器仍然是非constcontainer_那么变量名就不太幸运了。

T对于不是 的类型const, 和 都const 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];}
Run Code Online (Sandbox Code Playgroud)

澄清这两个部分后,您就可以构建一个工作示例。这是基于您的代码并经过简化以解决当前问题的代码:

#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";
    }
}
Run Code Online (Sandbox Code Playgroud)

还要看一下 和 的正确实现,operator!=因为不需要非const版本的begin()end()。你也可以在那里扔一个cbegin()and cend()。然后你必须努力添加回 const 迭代器案例。


顺便说一句,以防万一它给任何人带来一些困惑:在不久的将来,可能会添加std::span为 C++20 提议的);而且它只是一(pointer-to-first-element, index)对——而不是你的(pointer-to-container, index)版本。

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

span<std::vector<int>> s(v);
// vs
std::span<int> s(v);
Run Code Online (Sandbox Code Playgroud)

这使得消费者可以避免std::span知道哪个容器在幕后(甚至没有容器:连续的内存区域或数组)。

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