【问题标题】:requirements for custom container type to use with views与视图一起使用的自定义容器类型的要求
【发布时间】:2020-12-26 14:45:59
【问题描述】:

我开始使用std::ranges 并想了解视图的真正工作原理。所以我尝试编写自己的容器和迭代器类型,并希望在视图中使用它。

但似乎缺少一些东西,但编译器只告诉我视图中没有 begin() 方法,但没有告诉我原因。

例子:

#include <iostream>
#include <array>
#include <ranges>

class MyFixedContainer;
class MyIterator
{
    MyFixedContainer* ptr;
    unsigned int offset;
public:
    MyIterator( MyFixedContainer* ptr_, unsigned int offset_ ): ptr{ ptr_},offset{offset_}{}

    bool operator==( MyIterator& other ) const
    {   
        return ( ptr == other.ptr )&& ( offset == other.offset );
    }   

    bool operator!=( MyIterator& other ) const
    {   
        return !(*this == other);
    }   

    MyIterator operator++()
    {   
        offset++;
        return *this;
    }   

    MyIterator operator++(int)
    {   
        MyIterator tmp = *this;
        offset++;
        return tmp;
    }   

    int operator*() const;
};

class MyFixedContainer
{
    std::array<int,4> arr={5,6,7,8};
public:
    auto begin() { return MyIterator{ this, 0 }; }
    auto end() { return MyIterator{ this, 4}; }

    int Get( int offset ) const
    {
        return arr[ offset ];
    }
};

int MyIterator::operator*() const
{
    return ptr->Get( offset );
}

int main()
{
    MyFixedContainer c;

    // Container type itself works:
    for ( int i: c )
    {
        std::cout << i << std::endl;
    }

    // Try to use with std::ranges
    auto even = [] (int i) { return 0 == i % 2; };

    auto y = std::views::filter(c, even);
    auto b = y.begin(); // << error message
}

编译

main.cpp:90:16: 错误:'struct std::ranges::views::__adaptor::_RangeAdaptorClosurestd::ranges::views::__adaptor::_RangeAdaptor<:operator main>&}>:: >' 没有名为 'begin' 的成员 90 |自动 b = y.begin();

https://godbolt.org/z/doW76j

【问题讨论】:

  • 通过编译您的代码(添加包含后),我收到的第一条消息是 'MyFixedContainer' does not name a type 。 wandbox.org/permlink/XUwpnVHerzhb9q1R
  • @克劳斯,我知道,我只是指出如果我们可以简单地复制&粘贴&编译并得到同样的错误会更好。
  • 基本上是我以前的question的骗子。

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


【解决方案1】:

MyIterator 不模拟 std::input_or_output_iterator 因为:

  • 它必须是默认可构造的。
  • std::iter_difference_t&lt;MyIterator&gt; 必须有效,并且
  • 前置自增运算符必须返回一个引用。

MyIterator 不是 std::sentinel_for&lt;MyIterator, MyIterator&gt;,因为它的运算符 == 和 != 采用引用而不是 const 引用。

MyIterator 不满足std::input_iterator,这要求std::iter_value_t 有效。

解决以上所有问题:

#include <iostream>
#include <array>
#include <ranges>

class MyFixedContainer;
class MyIterator
{
    MyFixedContainer* ptr;
    unsigned int offset;
public:
    using difference_type = int;
    using value_type = int;
    
    MyIterator() = default;
    
    MyIterator( MyFixedContainer* ptr_, unsigned int offset_ ): ptr{ ptr_},offset{offset_}{}

    bool operator==( MyIterator const & other ) const
    {   
        return ( ptr == other.ptr )&& ( offset == other.offset );
    }   

    bool operator!=( MyIterator const & other ) const
    {   
        return !(*this == other);
    }   

    MyIterator &operator++()
    {   
        offset++;
        return *this;
    }   

    MyIterator operator++(int)
    {   
        MyIterator tmp = *this;
        offset++;
        return tmp;
    }   

    int operator*() const;
};

class MyFixedContainer
{
    std::array<int,4> arr={5,6,7,8};
public:
    auto begin() { return MyIterator{ this, 0 }; }
    auto end()   { return MyIterator{ this, 4}; }

    int Get( int offset ) const
    {
        return arr[ offset ];
    }
};

int MyIterator::operator*() const
{
    return ptr->Get( offset );
}

int main()
{
    MyFixedContainer c;

    // Container type itself works:
    for ( int i: c )
    {
        std::cout << i << std::endl;
    }

    // Try to use with std::ranges
    auto even = [] (int i) { return 0 == i % 2; };

    static_assert(std::input_or_output_iterator<MyIterator>);
    static_assert(std::ranges::input_range<MyFixedContainer>);
    
    auto y = c | std::views::filter(even);
    
    auto b = y.begin(); // << OK
}

如果您 static_assert 您的容器/迭代器必须建模的每个概念,错误消息会更加清晰。

【讨论】:

  • "如果您使用 static_assert,错误消息会更清晰"...但是我的问题开始了:必须满足哪个概念...?现在我得到了你的答案并且可以处理它。非常感谢!
  • @Klaus 哦,一点也不难。假设您希望您的容器成为std::ranges::random_access_range。你只需要看看它包含哪些概念。感谢概念(以及 C. Carter 和 E. Niebler 等人的辛勤工作),现在算法库是自记录的。与之前的现状相比,这是一个重大改进。
猜你喜欢
  • 1970-01-01
  • 2012-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-08
  • 1970-01-01
  • 2019-06-28
相关资源
最近更新 更多