问题描述
为什么此代码可以与 #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 << "]\n";
}
#if 0
{
auto tmp = skip_after(after_e) | take(6);
cout << '[';
copy(sv | tmp, ostream_iterator<char>(cout));
cout << "]\n";
}
#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.
这篇关于您如何创建自己的视图,以便通过运算符|与现有视图进行交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!