* [Bug libstdc++/51823] reverse iterator instantiated with POD type returns uninitialized values + work around
2012-01-11 12:58 [Bug libstdc++/51823] New: reverse iterator instantiated with POD type returns uninitialized values + work around jakobsybren at gmail dot com
@ 2012-01-11 13:16 ` redi at gcc dot gnu.org
2012-01-11 13:40 ` jakobsybren at gmail dot com
` (15 subsequent siblings)
16 siblings, 0 replies; 18+ messages in thread
From: redi at gcc dot gnu.org @ 2012-01-11 13:16 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51823
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-01-11 13:16:16 UTC ---
(In reply to comment #0)
> Consider an iterator class that implements the requirements of a
> forward_iterator. We underlying datatype is always POD and the operator*()
> member of the iterator returns the results by value, since it is POD anyway.
The iterator requirements [iterator.iterators] say operator* must return
iterator_traits<some_iterator>::reference
^ permalink raw reply [flat|nested] 18+ messages in thread
* [Bug libstdc++/51823] reverse iterator instantiated with POD type returns uninitialized values + work around
2012-01-11 12:58 [Bug libstdc++/51823] New: reverse iterator instantiated with POD type returns uninitialized values + work around jakobsybren at gmail dot com
2012-01-11 13:16 ` [Bug libstdc++/51823] " redi at gcc dot gnu.org
@ 2012-01-11 13:40 ` jakobsybren at gmail dot com
2012-01-11 13:56 ` redi at gcc dot gnu.org
` (14 subsequent siblings)
16 siblings, 0 replies; 18+ messages in thread
From: jakobsybren at gmail dot com @ 2012-01-11 13:40 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51823
Jakob van Bethlehem <jakobsybren at gmail dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Attachment #26299|0 |1
is obsolete| |
--- Comment #2 from Jakob van Bethlehem <jakobsybren at gmail dot com> 2012-01-11 13:40:18 UTC ---
Created attachment 26300
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=26300
Minimal example that reproduces bug, but with proper call-signature for
operator*() (ie: returning reference)
Minimal example was changed with proper call-signature for operator*(),
returning reference.
The resulting behavior remains the same as explained in the original message
though.
^ permalink raw reply [flat|nested] 18+ messages in thread
* [Bug libstdc++/51823] reverse iterator instantiated with POD type returns uninitialized values + work around
2012-01-11 12:58 [Bug libstdc++/51823] New: reverse iterator instantiated with POD type returns uninitialized values + work around jakobsybren at gmail dot com
2012-01-11 13:16 ` [Bug libstdc++/51823] " redi at gcc dot gnu.org
2012-01-11 13:40 ` jakobsybren at gmail dot com
@ 2012-01-11 13:56 ` redi at gcc dot gnu.org
2012-01-11 14:08 ` redi at gcc dot gnu.org
` (13 subsequent siblings)
16 siblings, 0 replies; 18+ messages in thread
From: redi at gcc dot gnu.org @ 2012-01-11 13:56 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51823
--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-01-11 13:55:49 UTC ---
(In reply to comment #0)
> Headerfiles involved:
> <iterator>
And <iostream>
> Consider an iterator class that implements the requirements of a
> forward_iterator.
Do you mean bidirectional iterator? You can't reverse a forward iterator, and
std::reverse_iterator requires that its template parameter meets the
requirements of a Bidirectional Iterator.
Your class doesn't implement those requirements, it doesn't have a
post-increment operator.
And as I said in my previous comment, the iterator requirements say operator*
returns the iterator's 'reference' type. reverse_iterator::operator* then
returns the same thing because the 'reference' type is, in effect, defined as
"the type returned by operator*"
(Also, the name __traits is forbidden, names beginning with double-underscore
are reserved for the implementation)
> The 'work-around' forces the use of forwarding. I put it as comment in the
> minimum example. If I should have attached a separate source file with just the
> operator*()-line replaced, I wish to express my apologies in advance.
The preprocessor strips comments out, so the workaround isn't in the attached
file.
The first problem is that your class lies, saying its 'reference' type is a
reference, but then returning something different from operator*. That causes
a reference to be bound to a temporary, as you'll see if you compile with
-Wsystem-headers to enable warnings for code in the standard library headers.
The second problem is how reverse_iterator::operator* is defined:
reference
operator*() const
{
_Iterator __tmp = current;
return *--__tmp;
}
This creates a temporary iterator, then returns a reference to its d_val
member. By the time that's written to std::cout the temporary has been
destroyed.
For a bidirectional iterator the expression *r-- is required to be valid, so
your type fails that requirement.
What you've written is not an iterator, it's a generator. It doesn't meet the
iterator requirements, so can't be used with std::reverse_iterator and
std::copy and other templates that require iterators.
^ permalink raw reply [flat|nested] 18+ messages in thread
* [Bug libstdc++/51823] reverse iterator instantiated with POD type returns uninitialized values + work around
2012-01-11 12:58 [Bug libstdc++/51823] New: reverse iterator instantiated with POD type returns uninitialized values + work around jakobsybren at gmail dot com
` (2 preceding siblings ...)
2012-01-11 13:56 ` redi at gcc dot gnu.org
@ 2012-01-11 14:08 ` redi at gcc dot gnu.org
2012-01-11 14:19 ` redi at gcc dot gnu.org
` (12 subsequent siblings)
16 siblings, 0 replies; 18+ messages in thread
From: redi at gcc dot gnu.org @ 2012-01-11 14:08 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51823
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |INVALID
--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-01-11 14:06:36 UTC ---
(In reply to comment #3)
> The preprocessor strips comments out, so the workaround isn't in the attached
> file.
Fixed by the new attachment
> For a bidirectional iterator the expression *r-- is required to be valid, so
> your type fails that requirement.
Not fixed, this invalid.
^ permalink raw reply [flat|nested] 18+ messages in thread
* [Bug libstdc++/51823] reverse iterator instantiated with POD type returns uninitialized values + work around
2012-01-11 12:58 [Bug libstdc++/51823] New: reverse iterator instantiated with POD type returns uninitialized values + work around jakobsybren at gmail dot com
` (3 preceding siblings ...)
2012-01-11 14:08 ` redi at gcc dot gnu.org
@ 2012-01-11 14:19 ` redi at gcc dot gnu.org
2012-01-11 14:37 ` redi at gcc dot gnu.org
` (11 subsequent siblings)
16 siblings, 0 replies; 18+ messages in thread
From: redi at gcc dot gnu.org @ 2012-01-11 14:19 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51823
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|RESOLVED |NEW
Last reconfirmed| |2012-01-11
Resolution|INVALID |
Ever Confirmed|0 |1
--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-01-11 14:17:34 UTC ---
actually, my bad, even if your type did meet the requirements it wouldn't work,
and that *is* a libstdc++ bug
at some point between C++03 and C++11 the definition of
reverse_iterator::operator* was changed to clarify this, making libstdc++'s
implementation wrong. The standard now says:
[ Note: This operation must use an auxiliary member variable rather than a
temporary variable to avoid returning a reference that persists beyond the
lifetime of its associated iterator. (See 24.2.) —end note ]
^ permalink raw reply [flat|nested] 18+ messages in thread
* [Bug libstdc++/51823] reverse iterator instantiated with POD type returns uninitialized values + work around
2012-01-11 12:58 [Bug libstdc++/51823] New: reverse iterator instantiated with POD type returns uninitialized values + work around jakobsybren at gmail dot com
` (4 preceding siblings ...)
2012-01-11 14:19 ` redi at gcc dot gnu.org
@ 2012-01-11 14:37 ` redi at gcc dot gnu.org
2012-01-11 15:17 ` marc.glisse at normalesup dot org
` (10 subsequent siblings)
16 siblings, 0 replies; 18+ messages in thread
From: redi at gcc dot gnu.org @ 2012-01-11 14:37 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51823
--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-01-11 14:37:27 UTC ---
That change was present as early as the Feb 2004 draft, n1577
Also related: http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#1052
^ permalink raw reply [flat|nested] 18+ messages in thread
* [Bug libstdc++/51823] reverse iterator instantiated with POD type returns uninitialized values + work around
2012-01-11 12:58 [Bug libstdc++/51823] New: reverse iterator instantiated with POD type returns uninitialized values + work around jakobsybren at gmail dot com
` (5 preceding siblings ...)
2012-01-11 14:37 ` redi at gcc dot gnu.org
@ 2012-01-11 15:17 ` marc.glisse at normalesup dot org
2012-01-11 15:26 ` [Bug libstdc++/51823] [DR 198] reverse iterator returns uninitialized values redi at gcc dot gnu.org
` (9 subsequent siblings)
16 siblings, 0 replies; 18+ messages in thread
From: marc.glisse at normalesup dot org @ 2012-01-11 15:17 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51823
--- Comment #7 from Marc Glisse <marc.glisse at normalesup dot org> 2012-01-11 15:16:58 UTC ---
Paolo started a discussion on this:
http://gcc.gnu.org/ml/libstdc++/2003-11/msg00089.html
but apparently the code wasn't changed. It is really sad that in most cases the
size of reverse_iterator is needlessly doubled just so some rare fancy
iterators can use it. And other fancy uses prevent from using a global instead
(even a tls).
For the special case of a Iota_iterator, when the C++ iterator concept is
eventually fixed (or for people who decide to ignore the requirement that
reference be a reference), reference should be int, not int const&, so
operator* would return by value.
^ permalink raw reply [flat|nested] 18+ messages in thread
* [Bug libstdc++/51823] [DR 198] reverse iterator returns uninitialized values
2012-01-11 12:58 [Bug libstdc++/51823] New: reverse iterator instantiated with POD type returns uninitialized values + work around jakobsybren at gmail dot com
` (6 preceding siblings ...)
2012-01-11 15:17 ` marc.glisse at normalesup dot org
@ 2012-01-11 15:26 ` redi at gcc dot gnu.org
2012-01-11 15:48 ` marc.glisse at normalesup dot org
` (8 subsequent siblings)
16 siblings, 0 replies; 18+ messages in thread
From: redi at gcc dot gnu.org @ 2012-01-11 15:26 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51823
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Summary|reverse iterator |[DR 198] reverse iterator
|instantiated with POD type |returns uninitialized
|returns uninitialized |values
|values + work around |
--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-01-11 15:25:42 UTC ---
Thanks, Marc, I'd failed to find the relevant DR number
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#198
We could specialize reverse_iterator on a trait that says the extra member
isn't needed, and then make that trait true for all iterator types defined in
the library. The size would still change for
reverse_iterator<user_defined_iterator> but I don't see how else to meet the
requirement.
^ permalink raw reply [flat|nested] 18+ messages in thread
* [Bug libstdc++/51823] [DR 198] reverse iterator returns uninitialized values
2012-01-11 12:58 [Bug libstdc++/51823] New: reverse iterator instantiated with POD type returns uninitialized values + work around jakobsybren at gmail dot com
` (7 preceding siblings ...)
2012-01-11 15:26 ` [Bug libstdc++/51823] [DR 198] reverse iterator returns uninitialized values redi at gcc dot gnu.org
@ 2012-01-11 15:48 ` marc.glisse at normalesup dot org
2012-01-11 15:59 ` redi at gcc dot gnu.org
` (7 subsequent siblings)
16 siblings, 0 replies; 18+ messages in thread
From: marc.glisse at normalesup dot org @ 2012-01-11 15:48 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51823
--- Comment #9 from Marc Glisse <marc.glisse at normalesup dot org> 2012-01-11 15:46:44 UTC ---
(In reply to comment #8)
> We could specialize reverse_iterator on a trait that says the extra member
> isn't needed, and then make that trait true for all iterator types defined in
> the library.
I was just writing myself a note to try and get such a trait added to the
standard ;-)
I don't know if it is worth doing that optimization now (I was complaining on
the principle, I don't think I ever used reverse_iterator), depends on your
motivation...
^ permalink raw reply [flat|nested] 18+ messages in thread
* [Bug libstdc++/51823] [DR 198] reverse iterator returns uninitialized values
2012-01-11 12:58 [Bug libstdc++/51823] New: reverse iterator instantiated with POD type returns uninitialized values + work around jakobsybren at gmail dot com
` (8 preceding siblings ...)
2012-01-11 15:48 ` marc.glisse at normalesup dot org
@ 2012-01-11 15:59 ` redi at gcc dot gnu.org
2012-01-14 16:38 ` redi at gcc dot gnu.org
` (6 subsequent siblings)
16 siblings, 0 replies; 18+ messages in thread
From: redi at gcc dot gnu.org @ 2012-01-11 15:59 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51823
--- Comment #10 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-01-11 15:59:43 UTC ---
I can work on it for GCC 4.8
^ permalink raw reply [flat|nested] 18+ messages in thread
* [Bug libstdc++/51823] [DR 198] reverse iterator returns uninitialized values
2012-01-11 12:58 [Bug libstdc++/51823] New: reverse iterator instantiated with POD type returns uninitialized values + work around jakobsybren at gmail dot com
` (9 preceding siblings ...)
2012-01-11 15:59 ` redi at gcc dot gnu.org
@ 2012-01-14 16:38 ` redi at gcc dot gnu.org
2012-08-22 19:19 ` redi at gcc dot gnu.org
` (5 subsequent siblings)
16 siblings, 0 replies; 18+ messages in thread
From: redi at gcc dot gnu.org @ 2012-01-14 16:38 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51823
--- Comment #11 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-01-14 15:43:52 UTC ---
Created attachment 26323
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=26323
implement DR 198
it's ugly, but it works
^ permalink raw reply [flat|nested] 18+ messages in thread
* [Bug libstdc++/51823] [DR 198] reverse iterator returns uninitialized values
2012-01-11 12:58 [Bug libstdc++/51823] New: reverse iterator instantiated with POD type returns uninitialized values + work around jakobsybren at gmail dot com
` (10 preceding siblings ...)
2012-01-14 16:38 ` redi at gcc dot gnu.org
@ 2012-08-22 19:19 ` redi at gcc dot gnu.org
2012-10-30 23:49 ` dave at boostpro dot com
` (4 subsequent siblings)
16 siblings, 0 replies; 18+ messages in thread
From: redi at gcc dot gnu.org @ 2012-08-22 19:19 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51823
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Keywords| |ABI
--- Comment #12 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-08-22 19:19:15 UTC ---
setting ABI keyword as this can't be properly fixed without an incompatible
change
^ permalink raw reply [flat|nested] 18+ messages in thread
* [Bug libstdc++/51823] [DR 198] reverse iterator returns uninitialized values
2012-01-11 12:58 [Bug libstdc++/51823] New: reverse iterator instantiated with POD type returns uninitialized values + work around jakobsybren at gmail dot com
` (11 preceding siblings ...)
2012-08-22 19:19 ` redi at gcc dot gnu.org
@ 2012-10-30 23:49 ` dave at boostpro dot com
2012-10-31 0:37 ` redi at gcc dot gnu.org
` (3 subsequent siblings)
16 siblings, 0 replies; 18+ messages in thread
From: dave at boostpro dot com @ 2012-10-30 23:49 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51823
Dave Abrahams <dave at boostpro dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |dave at boostpro dot com
--- Comment #13 from Dave Abrahams <dave at boostpro dot com> 2012-10-30 23:48:36 UTC ---
I think this bug is invalid, because the iterator being wrapped doesn't model
ForwardIterator due to I found 24.2.5 [forward.iterators]/6, where it says:
If a and b are both dereferenceable, then a == b if and only if *a and
*b are bound to the same object.
^ permalink raw reply [flat|nested] 18+ messages in thread
* [Bug libstdc++/51823] [DR 198] reverse iterator returns uninitialized values
2012-01-11 12:58 [Bug libstdc++/51823] New: reverse iterator instantiated with POD type returns uninitialized values + work around jakobsybren at gmail dot com
` (12 preceding siblings ...)
2012-10-30 23:49 ` dave at boostpro dot com
@ 2012-10-31 0:37 ` redi at gcc dot gnu.org
2012-12-13 12:24 ` [Bug libstdc++/51823] [DR 198] [DR 2204] " redi at gcc dot gnu.org
` (2 subsequent siblings)
16 siblings, 0 replies; 18+ messages in thread
From: redi at gcc dot gnu.org @ 2012-10-31 0:37 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51823
--- Comment #14 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-10-31 00:36:48 UTC ---
Thanks, Dave. So IIUC that means a counting iterator or other iterator that
stores a value internally can only be an input iterator, and so can't be used
with reverse_iterator.
^ permalink raw reply [flat|nested] 18+ messages in thread
* [Bug libstdc++/51823] [DR 198] [DR 2204] reverse iterator returns uninitialized values
2012-01-11 12:58 [Bug libstdc++/51823] New: reverse iterator instantiated with POD type returns uninitialized values + work around jakobsybren at gmail dot com
` (13 preceding siblings ...)
2012-10-31 0:37 ` redi at gcc dot gnu.org
@ 2012-12-13 12:24 ` redi at gcc dot gnu.org
2012-12-14 16:35 ` dave at boostpro dot com
2014-02-19 21:02 ` redi at gcc dot gnu.org
16 siblings, 0 replies; 18+ messages in thread
From: redi at gcc dot gnu.org @ 2012-12-13 12:24 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51823
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |SUSPENDED
Summary|[DR 198] reverse iterator |[DR 198] [DR 2204] reverse
|returns uninitialized |iterator returns
|values |uninitialized values
--- Comment #15 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-12-13 12:23:46 UTC ---
Suspending pending resolution of Dave's issue:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3473.html#2204
^ permalink raw reply [flat|nested] 18+ messages in thread
* [Bug libstdc++/51823] [DR 198] [DR 2204] reverse iterator returns uninitialized values
2012-01-11 12:58 [Bug libstdc++/51823] New: reverse iterator instantiated with POD type returns uninitialized values + work around jakobsybren at gmail dot com
` (14 preceding siblings ...)
2012-12-13 12:24 ` [Bug libstdc++/51823] [DR 198] [DR 2204] " redi at gcc dot gnu.org
@ 2012-12-14 16:35 ` dave at boostpro dot com
2014-02-19 21:02 ` redi at gcc dot gnu.org
16 siblings, 0 replies; 18+ messages in thread
From: dave at boostpro dot com @ 2012-12-14 16:35 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51823
--- Comment #16 from Dave Abrahams <dave at boostpro dot com> 2012-12-14 16:34:31 UTC ---
Normative text vs. non-normative note == no contest, IMO. But I guess it
doesn't hurt to have the bug open if it doesn't mean any changes to the
library.
^ permalink raw reply [flat|nested] 18+ messages in thread
* [Bug libstdc++/51823] [DR 198] [DR 2204] reverse iterator returns uninitialized values
2012-01-11 12:58 [Bug libstdc++/51823] New: reverse iterator instantiated with POD type returns uninitialized values + work around jakobsybren at gmail dot com
` (15 preceding siblings ...)
2012-12-14 16:35 ` dave at boostpro dot com
@ 2014-02-19 21:02 ` redi at gcc dot gnu.org
16 siblings, 0 replies; 18+ messages in thread
From: redi at gcc dot gnu.org @ 2014-02-19 21:02 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51823
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|SUSPENDED |RESOLVED
Resolution|--- |INVALID
--- Comment #17 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The definition of reverse_iterator has been reverted to the C++03 version,
which does not use an extra member, so "stashing iterators" can not be used
with reverse_iterator.
^ permalink raw reply [flat|nested] 18+ messages in thread