您如何创建自己的视图,并使用运算符 | 与现有视图交互?

How you create your own views that interact with existing views with operator |?(您如何创建自己的视图,并使用运算符 | 与现有视图交互?)

本文介绍了您如何创建自己的视图,并使用运算符 | 与现有视图交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这段代码可以与 #if 0 块一起工作,但如果删除它会失败并显示一组相当复杂的错误消息?更重要的是,如何使它与上面非常相似的块的结果相同?

Why does this code work with the #if 0 block in place, but fails with a fairly complex set of error messages if you remove it? And more importantly, how do I make it the same result as the very similar block above it?

#include <ranges>
#include <iterator>
#include <optional>
#include <string_view>
#include <iostream>
#include <algorithm>

template <::std::ranges::view View,
          typename Pred>
requires ::std::ranges::input_range<View> &&
         ::std::ranges::common_range<View> &&
         ::std::is_object_v<Pred> &&
         ::std::indirect_unary_predicate<const Pred, ::std::ranges::iterator_t<View>>
class skip_after_view : public ::std::ranges::view_interface<skip_after_view<View, Pred>>
{
 public:
   skip_after_view() = default;
   skip_after_view(View v, Pred p)
        : subview_(::std::move(v)), pred_(::std::move(p))
   {}
   class iterator;
   friend class iterator;

   auto begin() const {
       return iterator{subview_.begin(), subview_.end(), &pred_};
   }
   auto end() const {
       return iterator{subview_.end(), subview_.end(), &pred_};
   }

 private:
   View subview_ = View();
   Pred pred_;
};

template <typename View, typename Pred>
class skip_after_view<View, Pred>::iterator
{
   using parent_t = View::iterator;
   using parent_traits = ::std::iterator_traits<parent_t>;
   friend class skip_after_view<View, Pred>;
 public:
   using value_type = parent_traits::value_type;
   using reference = parent_traits::reference;
   using pointer = parent_traits::pointer;
   using difference_type = ::std::ptrdiff_t;
   using iterator_category = ::std::input_iterator_tag;

   constexpr iterator() = default;

   auto operator *() { return *me_; }
   auto operator *() const { return *me_; }
   iterator &operator ++() {
       for (bool last_pred = true; last_pred; ) {
          if (end_ != me_) {
              last_pred = (*pred_)(operator *());
              ++me_;
          } else {
              last_pred = false;
          }
       }
       return *this;       
   }
   void operator ++(int) {
       ++(*this);
   }
   friend
   bool operator ==(iterator const &a, iterator const &b) {
       return a.me_ == b.me_;
   }

 private:
   parent_t me_;
   parent_t end_;
   Pred const *pred_ = nullptr;

   iterator(parent_t const &me, parent_t end, Pred const *pred)
        : me_(me), end_(::std::move(end)), pred_(pred)
   {}
};

template <std::ranges::range Range, typename Pred>
skip_after_view(Range&&) -> skip_after_view<std::ranges::views::all_t<Range>, Pred>;

struct skip_after_adaptor {

   template <typename Pred>
   class closure {
       friend class skip_after_adaptor;
       Pred pred;

       explicit closure(Pred &&p) : pred(::std::move(p)) {}
     public:
       template <typename Range>
       auto operator ()(Range &&range) {
          return skip_after_view(::std::forward<Range>(range),
                                 ::std::move(pred));
       }
   };
   template <typename Pred>
   auto operator ()(Pred pred) const {
       return closure<Pred>(::std::move(pred));
   }
   template <typename Range, typename Pred>
   auto operator()(Range &&range, Pred &&pred) const {
       return skip_after_view(::std::forward(range), ::std::forward(pred));
   }
   template <typename Range, typename Pred>
   friend auto operator|(Range&& rng, closure<Pred> &&fun) {
       return fun(std::forward<Range>(rng));
    }
};

constexpr auto skip_after = skip_after_adaptor{};

template <::std::input_iterator it>
void check(it const &)
{}

int main()
{
    using ::std::string_view;
    using namespace ::std::ranges::views;
    using ::std::ostream_iterator;
    using ::std::ranges::copy;
    using ::std::cout;
    auto after_e = [](char c) { return c == 'e'; };

    constexpr string_view sv{"George Orwell"};
    int sum = 0;
    {
       cout << '[';
       copy(sv | skip_after(after_e) | take(6),
            ostream_iterator<char>(cout));
       cout << "]
";
    }
#if 0
    {
       auto tmp = skip_after(after_e) | take(6);
       cout << '[';
       copy(sv | tmp, ostream_iterator<char>(cout));
       cout << "]
";
    }
#endif
    return sum;
}

强制性编译器资源管理器链接

如果我想要的不是完全可能的,有什么丑陋的方法可以做到吗?例如,我是否可以制作自己的合成机制并使用一堆丑陋的垃圾来将其与现有视图接口.

If what I want isn't cleanly possible, is there an ugly way to do it? For example, could I make my own composition mechanism and have an ugly bunch of garbage to interface it with the existing views.

推荐答案

没有办法编写与 C++20 中的标准对象组合的范围适配器闭包对象.标准库没有公开它用于该组合的机制.

There's no way to write a range adapter closure object that composes with the standard ones in C++20. The standard library doesn't expose the mechanism it uses for that composition.

sv |skip_after(after_e) |take(6) 有效,因为 sv |skip_after(after_e) 会命中您的 operator|,它会生成一个 view,然后可以与 take(6) 一起使用.

sv | skip_after(after_e) | take(6) works because sv | skip_after(after_e) hits your operator|, which produces a view that can then be used with take(6).

C++23 很有可能会公开组合机制.

There's a good chance that C++23 will expose the composition mechanism.

这篇关于您如何创建自己的视图,并使用运算符 | 与现有视图交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:您如何创建自己的视图,并使用运算符 | 与现有视图交互?

基础教程推荐