This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/37172] New: g++ should not cast const iterator to non-const by default
- From: "nilmonid at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 20 Aug 2008 04:57:17 -0000
- Subject: [Bug c++/37172] New: g++ should not cast const iterator to non-const by default
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
The following c++ source file compiled by gcc without any warnings/errors with
the flags "-c -W -Wall" ->
// -------------------- start -----------------------
#include<string>
#include<set>
using namespace std;
class A0
{
public:
typedef set<int> Groups;
typedef Groups::const_iterator ConstIterator;
typedef Groups::iterator Iterator;
public:
ConstIterator get() const { return values.begin(); }
Iterator get() { return values.begin(); }
private:
Groups values;
};
class A1
{
public:
A1(A0* b) : a(b) {}
void f1() { A0::ConstIterator itr = a->get(); }
void g1() { A0::Iterator itr = a->get(); } // error with vc++ 8.0
private:
const A0 *a;
};
// -------------------- end -----------------------
However, the same code causes visual c++ 8.0 (visual studio 2005) to report
compilation error in the function g1() since it refuses to cast a const
iterator to non-const by default.
It seems that the behavior of gcc is incorrect and vc++ 8.0 is doing the right
thing since in the function g1(), the const pointer 'a' is only allowed to
invoke the const version of A0::get() which in turn would return a const
iterator. Unfortunately, gcc is automatically casting the returned const
iterator to a non-const iterator.
--
Summary: g++ should not cast const iterator to non-const by
default
Product: gcc
Version: 4.3.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: nilmonid at gmail dot com
GCC build triplet: x86
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37172