このコードは、#if 0
ブロックを配置した状態では機能するのに、ブロックを削除するとかなり複雑な一連のエラー メッセージが表示されて失敗するのはなぜですか? さらに重要なことに、その上にある非常によく似たブロックと同じ結果を得るにはどうすればよいでしょうか?
#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;
}
私が望むことがきれいに不可能な場合、それを行う醜い方法はありますか? たとえば、独自の構成メカニズムを作成し、それを既存のビューとインターフェースするための醜いゴミの束を用意することはできますか?