【问题标题】:const, span, and iterator troubleconst、span 和迭代器的麻烦
【发布时间】:2018-05-03 22:19:17
【问题描述】:

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

之后,我尝试在容器上写一个span<T>。 对于不是 const 的类型 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&lt;T&gt; 不能修改容器。

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";
    }
}

【问题讨论】:

  • 您是要避免未定义的行为,还是要求用户在以未定义的行为方式使用It 时要注意?
  • 如果要编译,可以使用const_cast。例如:It&lt;self_type&gt; begin() const { return It&lt;self_type&gt;(*const_cast&lt;span*&gt;(this), 0); }
  • 当用户使用 span&lt;T&gt;const span&lt;T&gt; 可能是 const 的类型 T 时,我想避免未定义的行为。

标签: c++ c++11 constants


【解决方案1】:

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

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

不是真的,因为template&lt;typename C&gt; class It 中的C 不是实际容器,而是span&lt;V&gt;。换句话说,看看:

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

这里self_type 表示const span&lt;V&gt;,因此您返回的是It&lt;const span&lt;V&gt;&gt;。因此,您的迭代器可以做任何可以用const span 做的事情——但容器仍然不是const。那么变量名container_就不好说了。

对于不是const 的类型Tconst span&lt;T&gt;span&lt;T&gt; 都允许更改容器的内容。 const span&lt;const T&gt;span&lt;const T&gt; 都禁止更改容器的内容。

另外,既然你想让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!= 的正确实现,以及不需要begin()end() 的非const 版本。你也可以在那里扔一个cbegin()cend()。然后你必须重新添加 const 迭代器案例。


顺便说一句,以防它为任何人节省一些混乱:在不久的将来,可能会添加std::spanproposed 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 以获得一些关于如何完全实现它的灵感(包括关于范围的第二个模板参数)。

【讨论】:

  • 我不知道 const 方法可以返回非常量引用:isocpp.org/wiki/faq/…
  • 因此,对象的顶级 const 并不意味着绝对不能为对象更改任何内容。这只是意味着对象只能根据类的 const-version 进行更改。
  • 如果方法不为 const 重载,如果方法是 const,则 const 对象和非 const 对象都将使用相同的方法。
【解决方案2】:

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

这允许对std::vector&lt;T&gt;span 使用相同的迭代器模板。

这两种情况是不同的。 对于非常量类型Tconst std::vector&lt;T&gt; 禁止更改其元素。 const span&lt;T&gt; 允许更改其元素。

主要区别在于span 类中的It&lt;const self_type&gt;(*this, 0);S&amp; span_; 而不是It 类中的const S&amp; span_;

修复:

#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";
}

【讨论】:

  • 如果span_ 实际上是std::vector&lt;T&gt;,则检查&amp;span_ != &amp;rhs.span_ 是否相等的代价很高。如果只检查span_&amp;rhs.span_ 是否实际上指的是内存中的相同地址并且具有相同的大小,也许可以?但这也可能导致其他问题......也许我可以添加一个断言来禁止在两个不同的容器上比较迭代器。
  • 另一个问题是从可变访问范围的 begin() 方法生成的迭代器到不可变访问范围的 begin() 方法生成的迭代器的类型转换。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-01
  • 1970-01-01
相关资源
最近更新 更多