Title
Missing Preconditions for take/drop adaptor
Status
new
Section
[range.take.overview][range.drop.overview]
Submitter
Hewill Kang

Created on 2025-02-15.00:00:00 last changed 1 month ago

Messages

Date: 2025-02-24.17:05:32

Proposed resolution:

This wording is relative to N5001 plus additions from P3471R4.

  1. Modify [range.take.overview] as indicated:

    -2- The name views::take denotes a range adaptor object ([range.adaptor.object]). Let E and F be expressions, let T be remove_cvref_t<decltype((E))>, and let D be range_difference_t<decltype((E))>. If decltype((F)) does not model convertible_to<D>, views::take(E, F) is ill-formed. Otherwise, the expression views::take(E, F) is expression-equivalent to:

    1. (2.?) Hardened preconditions: static_cast<D>(F) >= 0 is true.

    2. (2.1) — if T is a specialization of empty_view ([range.empty.view]), then ((void)F, decay-copy(E)), except that the evaluations of E and F are indeterminately sequenced.

  2. Modify [range.drop.overview] as indicated:

    -2- The name views::drop denotes a range adaptor object ([range.adaptor.object]). Let E and F be expressions, let T be remove_cvref_t<decltype((E))>, and let D be range_difference_t<decltype((E))>. If decltype((F)) does not model convertible_to<D>, views::drop(E, F) is ill-formed. Otherwise, the expression views::drop(E, F) is expression-equivalent to:

    1. (2.?) Hardened preconditions: static_cast<D>(F) >= 0 is true.

    2. (2.1) — if T is a specialization of empty_view ([range.empty.view]), then ((void)F, decay-copy(E)), except that the evaluations of E and F are indeterminately sequenced.

Date: 2025-02-24.00:00:00

[ 2025-02-24 ]

Upon reflector discussion many preferred to use instead the new Hardened preconditions: element which have been introduced by the recently voted in P3471R4.

Date: 2025-02-24.17:05:32

The take/drop adaptor does not explicitly require N to be non-negative (although the view class does), which makes it possible for some specialized cases to be well-defined when N is negative, since no Preconditions are violated:

auto e = std::views::empty<int> 
       | std::views::take(-1);   // []
auto i = std::views::iota(1, 5)
       | std::views::drop(-1);   // [0, 1, 2, 3, 4]
auto r = std::views::repeat('a', 2)
       | std::views::drop(-1);   // ['a', 'a', 'a']

This is not the intention, we should ban these cases.

This wording is relative to N5001.

  1. Modify [range.take.overview] as indicated:

    -2- The name views::take denotes a range adaptor object ([range.adaptor.object]). Let E and F be expressions, let T be remove_cvref_t<decltype((E))>, and let D be range_difference_t<decltype((E))>. If decltype((F)) does not model convertible_to<D>, views::take(E, F) is ill-formed. Otherwise, the expression views::take(E, F) is expression-equivalent to:

    1. (2.?) Preconditions: static_cast<D>(F) >= 0 is true.

    2. (2.1) — if T is a specialization of empty_view ([range.empty.view]), then ((void)F, decay-copy(E)), except that the evaluations of E and F are indeterminately sequenced.

  2. Modify [range.drop.overview] as indicated:

    -2- The name views::drop denotes a range adaptor object ([range.adaptor.object]). Let E and F be expressions, let T be remove_cvref_t<decltype((E))>, and let D be range_difference_t<decltype((E))>. If decltype((F)) does not model convertible_to<D>, views::drop(E, F) is ill-formed. Otherwise, the expression views::drop(E, F) is expression-equivalent to:

    1. (2.?) Preconditions: static_cast<D>(F) >= 0 is true.

    2. (2.1) — if T is a specialization of empty_view ([range.empty.view]), then ((void)F, decay-copy(E)), except that the evaluations of E and F are indeterminately sequenced.

History
Date User Action Args
2025-02-24 17:05:32adminsetmessages: + msg14659
2025-02-23 15:37:26adminsetmessages: + msg14656
2025-02-15 00:00:00admincreate