This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: Possible bug in gcc dealing with string class and compiler temps
- To: brax at ieeecs dot ece dot utexas dot edu
- Subject: Re: Possible bug in gcc dealing with string class and compiler temps
- From: "Martin v. Loewis" <martin at loewis dot home dot cs dot tu-berlin dot de>
- Date: Mon, 14 Feb 2000 23:19:59 +0100
- CC: gcc-bugs at gcc dot gnu dot org
- References: <Pine.LNX.3.96.1000214112953.14273A-102000@ieeecs.ece.utexas.edu>
> Please let me know if there is another work around for this, or if
> there is anything else that is not clear about this bug report
Thanks for your bug report. The problem appears to be that
std/bastring.h has
charT operator[] (size_type pos) const
{
if (pos == length ())
return eos ();
return data ()[pos];
}
so the const accessor returns a value, not a const reference. On an
abstract level, this is a known bug - the g++ library is not standard
compliant, yet (libstdc++ v3 will be).
I'm not sure what the impact of fixing that problem would be, so
unless you can propose a change with a discussion why it is a good
change (e.g. passes test suites), it is unlikely that this will be
fixed for gcc 2.95.
AFAICT, there are two work-arounds. One is to use at(), which is
implemented as
const_reference at (size_type pos) const
{
OUTOFRANGE (pos >= length ());
return data ()[pos];
}
This has the added effect of throwing out-of-range exceptions, which
should be no problem if you can guarantee in-range accesses.
The other work-around is to use the non-const accessor, as in
const char & foo::operator [] ( unsigned index ) const
{ return const_cast<string&>(mString) [ index ]; }
This has the added effect of calling selfish().
Hope this helps,
Martin