【问题标题】:What should I do to make my container work with ranges?我应该怎么做才能使我的容器与范围一起使用?
【发布时间】:2021-11-03 06:56:36
【问题描述】:

我有一个简单的容器:

template <class T, class Allocator = std::allocator<T>>
class ring
{
public:

    using value_type = T;
    using allocator_type = Allocator;
    using size_type = std::size_t;
    using difference_type = std::ptrdiff_t;
    using reference = T &;
    using const_reference = const T &;
    using pointer = T *;
    using const_pointer = const T *;

private:

    template <class E>
    class ring_iterator
    {
    public:

        using iterator_category = std::random_access_iterator_tag;
        using value_type = E;
        using difference_type = std::ptrdiff_t;
        using reference = E &;
        using pointer = E *;

        ring_iterator(const ring_iterator& other) = default;
        ring_iterator(ring_iterator&& other) = default;

        ring_iterator& operator = (const ring_iterator& other);

        pointer operator-> () const;

        reference operator* () const;

        ring_iterator& operator++ ();

        ring_iterator operator++ (int);

        ring_iterator& operator-- ();

        ring_iterator operator-- (int);

        ring_iterator& operator += (difference_type diff);

        ring_iterator& operator -= (difference_type diff);

        ring_iterator operator + (difference_type diff) const;

        ring_iterator operator - (difference_type diff) const;

        difference_type operator - (const ring_iterator& other) const;

        bool operator == (const ring_iterator& other) const;

        bool operator != (const ring_iterator& other)  const;

        bool operator < (const ring_iterator& other) const;

        operator ring_iterator<const E>() const;
    };

public:

    using iterator = ring_iterator<T>;
    using const_iterator = ring_iterator<const T>;
    using reverse_iterator = std::reverse_iterator<iterator>;
    using const_reverse_iterator = std::reverse_iterator<const_iterator>;

    ring(Allocator alloc = {});

    ring(size_type cap, Allocator alloc = {});

    ring(const ring& other);

    ring(ring&& other);

    ring& operator = (const ring& other);

    ring& operator = (ring&& other);

    ~ring();

    reference front();
    reference back();

    const_reference front() const;
    const_reference back() const;

    void push_front(const value_type& val);

    void push_front(value_type&& val);

    void push_back(const value_type& val);

    void push_back(value_type&& val);

    void pop_front();

    void pop_back();

    void reserve(size_t);

    void clear();

    size_type size() const;
    
    size_type capacity() const;
    
    bool empty() const;
    
    bool full() const;

    reference operator[](size_type index);

    const_reference operator[](size_type index) const;

    reference at(size_type index);

    const_reference at(size_type index) const;

    iterator begin();
    const_iterator begin() const;
    const_iterator cbegin() const;

    iterator end();
    const_iterator end() const;
    const_iterator cend() const;

    reverse_iterator rbegin();
    const_reverse_iterator rbegin() const;
    const_reverse_iterator crbegin() const;

    reverse_iterator rend();
    const_reverse_iterator rend() const;
    const_reverse_iterator crend() const;
};

我应该怎么做才能编译下面的代码?

#include <vector>
#include <ranges>

//std::vector<int> v; //compiles
ring<int> v;     //does not compile

auto range = v | std::views::transform([](int n) { return n * n; });

MSVC 编译器错误:

error C2678: binary '|': no operator found which takes a left-hand operand of type 'ring<int,std::allocator<int>>' (or there is no acceptable conversion)

【问题讨论】:

  • 所以...这是很多不必要的代码。首先,只需要声明即可查看您的类型是否适用于范围。其次,除了“标准方法”(如开始、结束)和数据成员之外的所有这些方法都是无关紧要的。
  • @bolov 更新了帖子。
  • ring_iterator 不需要是模板 - ring&lt;T&gt;::ring_iterator 已经依赖于T,它不需要是ring&lt;T&gt;::ring_iterator&lt;T&gt;
  • @Useless 为什么你认为 E 是 T?
  • 我指的是using iterator = ring_iterator&lt;T&gt;——嵌套类型已经依赖于外层模板参数,不需要再次模板化

标签: c++ c++20 std-ranges


【解决方案1】:

所以...经过大量调查:

您的迭代器必须有一个公共的默认构造函数。


我应该怎么做才能让我的容器使用范围?

应该满足std::ranges::range的概念:

static_assert(std::ranges::range<ring<int>>);

但它没有,错误消息也没有帮助。所以我们看看这个概念本身:

template< class T >
concept range = requires(T& t) {
  ranges::begin(t); // equality-preserving for forward iterators
  ranges::end  (t);
};

ranges::begin(v) 定义明确,但 ranges::end(v) 给出错误“错误:不匹配调用 '(const std::ranges::__cust_access::_End) (ring&)'”,同样没有有用的错误消息。

所以现在我们查看std::ranges::end

template< class T >
    requires /* see below */
constexpr std::sentinel_for<ranges::iterator_t<T>> auto end(T&& t);

这里的文档有点不确定,但这里失败的要求是:

static_assert(
    std::sentinel_for<ring<int>::iterator, ring<int>::iterator>
);

即结束迭代器应该是开始迭代器的标记。

这是我们第一个有用的错误消息到达的地方:

错误:静态断言失败

   89 |     std::sentinel_for<ring<int>::iterator, ring<int>::iterator>
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

注意:不满足约束

[...]

opt/compiler-explorer/gcc-trunk-20210906/include/c++/12.0.0/concepts:137:30: 注意:表达式is_constructible_v&lt;_Tp, _Args ...&gt; [with _Tp = ring&lt;int, std::allocator&lt;int&gt; &gt;::ring_iterator&lt;int&gt;; _Args = {}] 评估为“假”

  137 |       = destructible<_Tp> && is_constructible_v<_Tp, _Args...>;
      |                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

所以你有它ring&lt;int&gt;::ring_iterator&lt;int&gt; 必须有一个公开可用的默认构造函数。

【讨论】:

    【解决方案2】:

    我应该怎么做才能让我的容器使用范围?

    您应该设计容器以符合“范围”概念。

    简而言之,容器应该提供成员函数beginend,它们应该返回迭代器和哨兵。 end 标记必须可以从 begin 迭代器访问。哨兵类型可能与迭代器相同。迭代器类型必须符合“迭代器”的概念。

    我应该怎么做才能编译下面的代码?

    ring_iterator 在您的尝试中不是默认可初始化的,因此它不是迭代器,因此容器不是范围。添加一个默认构造函数来解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2021-12-14
      相关资源
      最近更新 更多