This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[RFH] libstdc++/6642
- From: Paolo Carlini <pcarlini at unitus dot it>
- To: libstdc++ at gcc dot gnu dot org
- Cc: Nathan Myers <ncm at nospam dot cantrip dot org>
- Date: Thu, 16 May 2002 14:41:26 +0200
- Subject: [RFH] libstdc++/6642
Hi everyone,
I'm taking a look at this PR, which definitely exercises my (poor)
understanding of the tricky iterator vs const_iterator issues... (=>
Effective STL Item 26-27 do not suffice for me to understand what is
really going on here :(
This is the testcase:
#include <string>
class MyClass {
private:
std::string s;
std::string::iterator it;
public:
unsigned pos() const {
return it - s.begin();
}
};
Which v3 (vs v2) does not compile, with the following error:
/usr/local/gcc/include/g++-v3/bits/stl_iterator.h: In constructor
`__gnu_cxx::__normal_iterator<_Iterator,
_Container>::__normal_iterator(const __gnu_cxx::__normal_iterator<_Iter,
_Container>&) [with _Iter = const char*, _Iterator = char*, _Container =
std::basic_string<char, std::char_traits<char>, std::allocator<char> >]':
6642.cc:9: instantiated from here
/usr/local/gcc/include/g++-v3/bits/stl_iterator.h:589: invalid
conversion from
`const char* const' to `char*'
Indeed, there are many workarounds: return - (s.begin - it) (as per
Scott Meyers hint), use std::string::const_iterator it, make pos()
non-const, etc...
However, from a theoretical point of view, I would like to really
understand what is at stake here!
Also, sadly, I have verified that many other implementations (v2,
STLPort, Dinkum, Metrowerks) are able to compile the testcase without
any problem at all.
Is there something we can do here while strictly following the ISO/ANSI
standard?
Thanks for your attention,
Paolo.