提问者:小点点

range-v3:修改已经实现迭代器接口的定制类(开始/结束)


我有一个实现< code>begin和< code>end的自定义容器。如何将这个容器通过管道连接到ranges-v3视图?

std::vector是可管道的,所以我尝试用同样的方法来管道化我的自定义类,但在我的容器中找不到管道操作符。

我查阅了文档,但是除了用Range接口重新实现一个包装类之外,我找不到任何其他方法。我有多个这样的类,我相信这可能是一个相当常见的情况,所以我宁愿使用库提供的一些函数(或类库),但我无法从文档中找到它。

下面是一个最小示例:

#include <iostream>
#include <iterator>
#include <range/v3/all.hpp>

struct Test {
    struct iterator;
    struct sentinel {};
    int counter;
    Test() = default;
    iterator begin();
    sentinel end() const { return {}; }
    iterator begin() const;
};

struct Test::iterator {
    using value_type = int;
    using reference = int&;
    using pointer = int*;
    using iterator_category = std::input_iterator_tag;
    using difference_type = void;
    Test* test;
    iterator& operator++() {
        test->counter++;
        return *this;
    }
    iterator operator++(int) {
        auto it = *this;
        ++*this;
        return it;
    }
    int operator*() { return test->counter; }
    int operator*() const { return test->counter; }
    bool operator!=(const iterator& rhs) const {
        return rhs.test != test;
    }
    bool operator!=(sentinel) const {
        return true;
    }
};

Test::iterator Test::begin() { return iterator {this}; }
Test::iterator Test::begin() const { return iterator {const_cast<Test*>(this)}; }

int main() {
    auto container = Test();
    static_assert(ranges::range<Test>, "It is not a range");
    static_assert(ranges::viewable_range<Test>, "It is not a viewable range");
    auto rng = container | ranges::views::take(10);
    for (auto n : rng) { std::cerr << n << std::endl;}
    return 0;
}

这是我使用此代码时遇到的错误:

~/tmp/range$ g++ main.cpp -Irange-v3/include -o main 2>&1 | grep error
main.cpp:46:19: error: static assertion failed: It is not a range
main.cpp:47:19: error: static assertion failed: It is not a viewable range
range-v3/include/range/v3/functional/pipeable.hpp:63:53: error: no matching function for call to ‘ranges::pipeable_access::impl<ranges::views::view<ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::take_fn, int>]::_> >::pipe(Test&, ranges::views::view<ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::take_fn, int>]::_>&)’
main.cpp:48:10: error: ‘void rng’ has incomplete type
main.cpp:49:19: error: unable to deduce ‘auto&&’ from ‘rng’

共2个答案

匿名用户

这是我的解决方案:

template <typename T>                                                                                                                                                                                              
class ContainerView : public ranges::view_facade<ContainerView<T>> {                                                                                                                                              
    friend ranges::range_access;                                                                                                                                                                                   

    using iterator_type = decltype(std::declval<T>().begin());                                                                                                                                                     
    using value_type = decltype(*std::declval<iterator_type>());                                                                                                                                                   

    T* container_;                                                                                                                                                                                                 
    iterator_type it_;                                                                                                                                                                                             

    value_type read() const { return *it_; }                                                                                                                                                                       
    bool equal(ranges::default_sentinel_t) const {                                                                                                                                                                 
        return !(it_ != container_->end());                                                                                                                                                                        
    }                                                                                                                                                                                                              
    void next() { ++it_; }                                                                                                                                                                                         

   public:                                                                                                                                                                                                         
    ContainerView() = default;                                                                                                                                                                                     
    ContainerView(T& container)                                                                                                                                                                                    
        : container_(&container), it_(container_->begin()) {}                                                                                                                                                      
    using ranges::view_facade<ContainerView<T>>::begin;                                                                                                                                                            
    using ranges::view_facade<ContainerView<T>>::end;                                                                                                                                                              
};                                                                                                                                                                                                                 

int main() {                                                                                                                                                                                                       
    auto container = Test();                                                                                                                                                                                       
    auto view = ContainerView(container);                                                                                                                                                                          
    static_assert(ranges::range<decltype(view)>, "It is not a range");                                                                                                                                             
    static_assert(ranges::viewable_range<decltype(view)>,                                                                                                                                                          
                  "It is not a viewable range");                                                                                                                                                                   
    auto rng = view | ranges::views::take(10);                                                                                                                                                                     
    for (auto n : rng) {                                                                                                                                                                                           
        std::cerr << n << std::endl;                                                                                                                                                                               
    }                                                                                                                                                                                                              
    return 0;                                                                                                                                                                                                      
}

我不确定我是否重新实现了库中已经实现的东西。所以,我暂时不会将此标记为答案。如果有现有的方法,请评论或发布新的答案。

匿名用户

  • 对sentinel_for进行建模,迭代器和哨兵必须在两个方向上使用 ==!= 进行比较。(输入迭代器在新世界中不必相互比较。您只提供了 != 并且仅在一个方向上提供。
  • difference_type对于输入迭代器不能无效。它必须是有符号整数类型,如 ptrdiff_t

此外:

  • 引用应该是运算符 * 的返回类型;它不必是引用类型。
  • viewable_range