ericniebler / stl2

LaTeX and Markdown source for the Ranges TS/STL2 and associated proposals

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

subrange: must end be reachable from begin?

ericniebler opened this issue · comments

Is it kosher to construct a subrange for which end is not reachable from begin? Say, if begin is reachable from end instead? And size() returns a negative number?

Probably not. If that is the case, we should require that [begin,end) denotes a valid range.

Proposed Resolution

Change [range.subrange.ctor] as follows:

 constexpr subrange(I i, S s) requires (!StoreSize);

+-?- Expects: [i, s) is a valid range.

 -1- Effects: Initializes begin_ with i and end_ with s.

 constexpr subrange(I i, S s, iter_difference_t<I> n)
   requires (K == subrange_kind::sized);
 
 -2- Expects: 
+    [i, s) is a valid range, and
     n == ranges::distance(i, s).

It it kosher to construct a subrange for which end is not reachable from begin?

Technically, yes it is. The resulting subrange wouldn't be in the domain of Range, however, since [range.range]/3.1 requires that "[ranges::begin(E), ranges::end(E)) denotes a range ([iterator.requirements.general])".

This seems like the sort of weirdness we should forbid.

PR for your perusal.

This issue is now LWG 3179.

Does this mean subrange(iota_view::iterator(0u), unreachable_sentinel) (or subrange(iota(0u))) is disallowed?

No, those are fine.

"Being a valid range" imposes the reachability condition, but [iota_view::iterator(0u), unreachable_sentinel) does not satisfy the reachability condition. Thus, after this PR is applied, subrange(iota_view::iterator(0u), unreachable_sentinel) becomes UB.

Or, I may be misunderstanding the validity of ranges / unreachable_sentinel.

We have a known issue regarding the definition of valid range in terms of reachability.

Ah, ok, understood. Thanks for the reply!