The const overloads of take_while_view::begin/end are underconstrained
Tim Song

Created on 2020-06-06.00:00:00 last changed 4 weeks ago


Date: 2020-06-06.13:46:40

Proposed resolution:

This wording is relative to N4861.

  1. Modify [range.take.while.view], class template take_while_view synopsis, as indicated:

    namespace std::ranges {
        template<view V, class Pred>
          requires input_range<V> && is_object_v<Pred> &&
                   indirect_unary_predicate<const Pred, iterator_t<V>>
        class take_while_view : public view_interface<take_while_view<V, Pred>> {
        constexpr auto begin() requires (!simple-view<V>)
        { return ranges::begin(base_); }
        constexpr auto begin() const requires range<const V> && indirect_unary_predicate<const Pred, iterator_t<const V>>
        { return ranges::begin(base_); }
        constexpr auto end() requires (!simple-view<V>)
        { return sentinel<false>(ranges::end(base_), addressof(*pred_)); }
        constexpr auto end() const requires range<const V> && indirect_unary_predicate<const Pred, iterator_t<const V>>
        { return sentinel<true>(ranges::end(base_), addressof(*pred_)); }
Date: 2020-06-06.00:00:00

The const overloads of take_while_view::begin and take_while_view::end are underconstrained: they should require the predicate to model indirect_unary_predicate<iterator_t<const V>>. A simple example is

    #include <ranges>

    auto v = std::views::single(1) | std::views::take_while([](int& x) { return true;});
    static_assert(std::ranges::range<decltype(v) const>);
    bool b = std::ranges::cbegin(v) == std::ranges::cend(v);

Here, the static_asserts pass but the comparison fails to compile. The other views using indirect_unary_predicate (filter_view and drop_while_view) do not have this problem because they do not support const-iteration.

Date User Action Args
2020-06-06 13:46:40adminsetmessages: + msg11325
2020-06-06 00:00:00admincreate