Bug 14410 - Bug with implementation of set for const iterators in g++ ...
Summary: Bug with implementation of set for const iterators in g++ ...
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 2.95.3
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 19480 37172 38304 41803 (view as bug list)
Depends on:
Blocks:
 
Reported: 2004-03-03 16:11 UTC by Ali Reza
Modified: 2009-10-22 23:21 UTC (History)
7 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2004-03-04 00:45:21


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ali Reza 2004-03-03 16:11:07 UTC
On g++ compiler extracted from cygwin, I found the following inconsistencies in 
the implementation of set.  If I have a const container, then the compiler 
should flag of a compilation *error* on an attempt to access elements using a 
non-const iterator. Hence, if I instantiate the print_elements method (which 
uses a non const iterator ) pos, I expect it to be *flagged*  of as a 
compilation error. This is not what happens. 

#include <iostream>
#include <set>
using namespace std;

template <typename T>
void print_elements(const T & t, const char * print_header)
{
  cout << print_header << endl;
  typename T::iterator pos;
  for (pos = t.begin(); pos != t.end(); ++pos) {
    cout << *pos << endl;
  }
}

This works perfectly fine if I instantiate using T as set<int> shown below. 
This is *incorrect*. 

int main()
{
  set<int> col1;
  for (int i = 0; i <=5; ++i) {
    col1.insert(i);
  }
  print_elements(col1, "initialized: ");
  return 0;
}

Could somebody explain ? 

BTW - compiler behaves as expected *flagging* off errors for other container's 
like deque, etc.
gcc --version 2.95.3-5
system type - Windows 2000 Professional
complete command line that triggers the bug - g++ bugged_prog.cc
Comment 1 Volker Reichelt 2004-03-04 00:45:19 UTC
Confirmed. Even in mainline's bits/stl_set.h we have

      typedef typename _Rep_type::const_iterator iterator;
      typedef typename _Rep_type::const_iterator const_iterator;
      typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
      typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;

which doesn't look right :-(
BTW, the same lines are in bits/stl_multiset.h.

If no-one beats me, I'll prepare a patch tomorrow.
Comment 2 Paolo Carlini 2004-03-04 01:12:24 UTC
The issue was already presented in the past, for instance in:

   http://gcc.gnu.org/ml/gcc-bugs/2003-02/msg01402.html

Basically, in the light of the resolution of DR 103 [WP], "...For associative
containers where the value type is the same as the key type, both iterator and
const_iterator are constant iterators...". Therefore, our current implementation
is conforming.
Comment 3 Volker Reichelt 2004-03-05 08:51:15 UTC
Subject: Re:  Bug with implementation of set for const
 iterators in g++ ...

On  4 Mar, pcarlini at suse dot de wrote:
> 
> ------- Additional Comments From pcarlini at suse dot de  2004-03-04 01:12 -------
> The issue was already presented in the past, for instance in:
> 
>    http://gcc.gnu.org/ml/gcc-bugs/2003-02/msg01402.html
> 
> Basically, in the light of the resolution of DR 103 [WP], "...For associative
> containers where the value type is the same as the key type, both iterator and
> const_iterator are constant iterators...". Therefore, our current implementation
> is conforming.

Oops! Sorry for the confusion.
But to prevent that from happening again, shouldn't we add a comment
like "DR 103" to the typedefs of the iterators in question?


Comment 4 Paolo Carlini 2004-03-05 09:04:27 UTC
Subject: Re:  Bug with implementation of set for const
 iterators in g++ ...

Volker Reichelt wrote:

>Oops! Sorry for the confusion.
>But to prevent that from happening again, shouldn't we add a comment
>like "DR 103" to the typedefs of the iterators in question?
>
In this specific case, I agree. But it's something that must be decided 
case by
case: we don't really want to add DR XXX for every DR that we implement
correctly, since many are trivial and/or have been always right.

Paolo.
Comment 5 Volker Reichelt 2004-03-20 21:58:07 UTC
Just for the record: Paolo added the suggested comment, see

http://gcc.gnu.org/ml/libstdc++-cvs/2004-q1/msg00636.html
Comment 6 Paolo Carlini 2005-01-17 12:07:37 UTC
*** Bug 19480 has been marked as a duplicate of this bug. ***
Comment 7 Paolo Carlini 2008-08-20 09:53:56 UTC
*** Bug 37172 has been marked as a duplicate of this bug. ***
Comment 8 Paolo Carlini 2008-11-28 15:11:57 UTC
*** Bug 38304 has been marked as a duplicate of this bug. ***
Comment 9 sleary@vavi.co.uk 2008-11-28 15:18:18 UTC
table 65 of container requirements states iterator points to non-const T. There is no exception for std::set.

Comment 10 Paolo Carlini 2008-11-28 15:21:51 UTC
Please refer to the resolution of DR 103, which we are implementing.
Comment 11 Paolo Carlini 2009-10-22 23:21:44 UTC
*** Bug 41803 has been marked as a duplicate of this bug. ***