const,span和Iterator的麻烦 [英] const, span, and iterator trouble

查看:37
本文介绍了const,span和Iterator的麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试编写一个通过索引对容器进行迭代的迭代器。
A It const It 都允许更改容器的内容。
Const_it const Const_it 都禁止更改容器的内容。

I try to write an iterator that iterates over a container by index. A It and a const It both allow changing the content of the container. A Const_it and a const Const_it both forbid changing the content of the container.

之后,我尝试在容器上写一个 span< T>
对于不是const的 T 类型, const span< T> span< T< code>允许更改容器的内容。
const span< const T> span< const T> 都禁止更改容器的内容

After that, I try to write a span<T> over a 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.

代码由于以下原因而无法编译:

The code does not compile because:

    // *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 容器,它看起来不正确,因为迭代器可以修改容器的内容。

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.

如果我摆脱了方法的const,那么对于非const类型T, const span< T> 无法修改容器。

If I get rid of the const of the method, then for a non-const type T, a const span<T> cannot modify the container.

继承自 Const_it 以允许从在模板实例化过程中 Const_it

It inherits from Const_it to allow implicit conversion from It to Const_it during template instantiation.

我使用一个指针,而不是迭代器中的引用( const C * container _; ),用于允许将一个迭代器分配给另一个迭代器。

I use a pointer instead of a reference to in the iterators (const C* container_;) for allowing assigning one iterator to another iterator.

我怀疑这里有些错误,因为我什至在想:

I suspect something is very wrong here because I even think about:

抛弃*的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";
    }
}


推荐答案

有有两个主要的注释可以说使这项工作有效。首先:

There are two main notes to be said to make this work. First:


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

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 不是实际的容器,而是 span< V> 。换句话说,请看:

Not really, because C in your template<typename C> class It is not the actual container, but the span<V>. In other words, take a look at:

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

此处 self_type 表示 const span< V> ,因此您返回的是 It< const span< V>> 。因此,您的迭代器可以执行 const span 所能做的一切-但容器仍然不是< const 。那么,变量名 container _ 并不幸运。

Here self_type means const span<V>, therefore you are returning a It<const span<V>>. Thus, your iterator can do whatever can be done with a const span -- but the container is still non-const. The variable name container_ is not fortunate, then.


对于类型<$不是 const 的c $ c> T const span< T> span< T> 允许更改容器的内容。 const span< const T> span< const T> 都禁止更改容器的内容。

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 ):

In addition, since you want that const span is allowed to modify the contents, then what you should write inside span itself is (note the 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];}

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

With those two bits clarified, you can then construct a working example. Here is one based from your code and simplified to solve the issue at hand:

#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()

Take a look as well at the corrected implementation of operator!= and at the fact that there is no need for the non-const version of begin() and end(). You can also throw there a cbegin() and cend(). Then you have to work on adding back the const iterator cases.

顺便说一句,以防为它节省一些混乱任何人:在不久的将来, std :: span 建议适用于C ++ 20 );而是一个(指针到第一个元素,索引)对-而不是您的(指针到容器,索引)版本。

By the way, in case it saves some confusion for anybody: in the near future, std::span (proposed for C++20) might be added; and it will be just a (pointer-to-first-element, index) pair -- rather than your (pointer-to-container, index) version.

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

In other words, as its template parameter, it will take the elements' type, rather than a container:

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

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

This allows consumers of std::span to avoid knowing about which container is behind the scenes (or even no container: a contiguous memory area or an array).

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

Finally, you may want to take a look at GSL's implementation of std::span to get some inspiration on how to fully implement it (including the second template parameter about the extent).

这篇关于const,span和Iterator的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆