Bug 70303 - Value-initialized debug iterators
Summary: Value-initialized debug iterators
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 6.0
: P3 normal
Target Milestone: 11.0
Assignee: François Dumont
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-03-19 01:22 UTC by Casey Carter
Modified: 2024-03-11 20:38 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2016-03-19 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Casey Carter 2016-03-19 01:22:56 UTC
Since C++14 requires value-initialized Forward iterators to compare equal, and subtraction/ordering of RandomAccess iterators is based on equality, this program should run to completion:

#define _GLIBCXX_DEBUG

#include <cassert>
#include <vector>

int main() {
  using I = std::vector<int>::iterator;
  assert(I{} == I{});
  assert(!(I{} != I{}));
  assert(I{} - I{} == 0);
  assert(!(I{} < I{}));
  assert(!(I{} > I{}));
  assert(I{} <= I{});
  assert(I{} >= I{});
}

instead of reporting "Error: attempt to compare a singular iterator to a singular iterator." for any of the listed iterator operations.
Comment 1 Jonathan Wakely 2016-03-19 18:40:28 UTC
Confirmed (fixing this would remove the "Partial" support noted at https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2014 :-)
Comment 2 Jonathan Wakely 2019-01-15 12:11:40 UTC
My reading of the N3644 changes is that only equality comparisons are supported, not relational ones.
Comment 3 Jonathan Wakely 2019-01-15 12:14:48 UTC
Or is the implication of equality being valid that a+n is valid for n==0, and therefore b-a is valid, and therefore relational ops are valid?
Comment 4 Casey Carter 2019-01-15 16:22:14 UTC
(In reply to Jonathan Wakely from comment #3)
> Or is the implication of equality being valid that a+n is valid for n==0,
> and therefore b-a is valid, and therefore relational ops are valid?

Certainly b-a is required to be valid, since such an n exists as required by [random.access.iterators] - but admittedly the IS doesn't specify the domain for relational comparisons on Cpp17 iterators. IMO this is a defect since it implies you can *never* compare two iterators with e.g. <. 

We should require the domain of relational comparisons to be the same as the domain of equality, which would then make it clear that value-initialized iterators are in their domain. (I was under the impression that we *did* require this "somewhere" when I filed this issue and fixed MSFTL's iterators.)
Comment 5 Casey Carter 2019-01-15 16:24:33 UTC
IIRC my reasoning was that [random.access.iterators] specifies the operational semantics of `a < b` to be `b - a > 0`, which suggests but doesn't quite require that `a < b` is valid whenever `b - a` is valid.
Comment 6 François Dumont 2021-01-27 20:58:39 UTC
After fixing the duplicate PR 98466 std::vector<int>::iterator is ok but std::deque<int>::iterator seems to be broken still.

Taking it.
Comment 7 François Dumont 2021-02-08 05:50:13 UTC
Main issue fixed as part of PR 98466 and 33a1e511b57465d898429740377466894a0b247d fixed the last part for deque::iterator - operator.