ericniebler / stl2

LaTeX and Markdown source for the Ranges TS/STL2 and associated proposals
88 stars 8 forks source link

subrange: must end be reachable from begin? #597

Closed ericniebler closed 5 years ago

ericniebler commented 5 years ago

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).
CaseyCarter commented 5 years ago

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.

CaseyCarter commented 5 years ago

This issue is now LWG 3179.

morinmorin commented 5 years ago

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

ericniebler commented 5 years ago

No, those are fine.

morinmorin commented 5 years ago

"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.

ericniebler commented 5 years ago

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

morinmorin commented 5 years ago

Ah, ok, understood. Thanks for the reply!