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
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.
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.
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?
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.
Just for the record: Paolo added the suggested comment, see http://gcc.gnu.org/ml/libstdc++-cvs/2004-q1/msg00636.html
*** Bug 19480 has been marked as a duplicate of this bug. ***
*** Bug 37172 has been marked as a duplicate of this bug. ***
*** Bug 38304 has been marked as a duplicate of this bug. ***
table 65 of container requirements states iterator points to non-const T. There is no exception for std::set.
Please refer to the resolution of DR 103, which we are implementing.
*** Bug 41803 has been marked as a duplicate of this bug. ***