Skip to content

subrange: must end be reachable from begin? #597

Description

@ericniebler

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

Activity

  1. changed the title [-]subrange: must end be reac[/-] [+]subrange: must end be reachable from begin?[/+] on Dec 19, 2018
  2. CaseyCarter commented on Dec 19, 2018

    @CaseyCarter
    Collaborator

    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.

  3. CaseyCarter commented on Dec 21, 2018

    @CaseyCarter
    Collaborator

    This issue is now LWG 3179.

  4. morinmorin commented on Dec 30, 2018

    @morinmorin

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

  5. ericniebler commented on Dec 30, 2018

    @ericniebler
    OwnerAuthor

    No, those are fine.

  6. morinmorin commented on Dec 30, 2018

    @morinmorin

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

  7. ericniebler commented on Dec 30, 2018

    @ericniebler
    OwnerAuthor

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

  8. morinmorin commented on Dec 30, 2018

    @morinmorin

    Ah, ok, understood. Thanks for the reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions