c++ - const、span 和迭代器问题

标签 c++ c++11 constants

我尝试编写一个迭代器,通过索引遍历容器。 It和一个 const It两者都允许更改容器的内容。 Const_it和一个 const Const_it两者都禁止改变容器的内容。

在那之后,我尝试写一个 span<T>在一个容器上。 对于类型 T这不是 const,两者都是 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); }

如果我创建 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";
    }
}

最佳答案

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

If I make the constructor of It to accept a const container, it doesn't look right because the iterator can modify the content of the container.

不是真的,因为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_那就不走运了。

For a type T that is not const, both const span<T> and span<T> allows changing the content of the container. Both const span<const T> and span<const T> forbid changing the content of the 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 迭代器案例。


顺便说一下,如果它能为任何人节省一些困惑:在不久的将来, std::span ( proposed for C++20 ) 可能会被添加;它只是一个 (pointer-to-first-element, index)对——而不是你的 (pointer-to-container, index)版本。

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

span<std::vector<int>> s(v);
// vs
std::span<int> s(v);

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

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

关于c++ - const、span 和迭代器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50164588/

相关文章:

c++ - 执行最后一行后出现堆栈错误

c++ - vector 增长时如何强制执行 move 语义?

c++ - 错误 : Class has not been declared despite header inclusion, 并且代码在其他地方编译正常

java - 在 JNI 下以 native 代码获取 java 缓冲区的最有效方法是什么?

c++ - 可变参数模板 lambda 扩展

C++ const 循环声明

函数声明中参数的 Const 限定

java - 为什么我们不能在 INTERFACE 的静态 block 内分配变量?奥卡

c++ - typedef 名称可以用于声明或定义构造函数吗?

c++ - 雪豹上的 python/c++ 包装