This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Possible bug in gcc dealing with string class and compiler temps


> 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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]