public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* confusion about string.erase( --string.end() )
@ 2011-11-22 17:23 Pawel Sikora
  2011-11-22 18:11 ` Marc Glisse
  2011-11-22 18:18 ` Ian Lance Taylor
  0 siblings, 2 replies; 3+ messages in thread
From: Pawel Sikora @ 2011-11-22 17:23 UTC (permalink / raw)
  To: gcc-help

Hi,

afaics the gcc-4.6 accepts following code:

#include <string>
int main()
{
        std::string line( "blabla" );
        line.erase( --line.end() );
}

but other compilers reject such code:

msvc8  : error C2105: '--' needs l-value
comeau : error: expression must be a modifiable lvalue

is it a bug in g++?

BR,
Paweł.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: confusion about string.erase( --string.end() )
  2011-11-22 17:23 confusion about string.erase( --string.end() ) Pawel Sikora
@ 2011-11-22 18:11 ` Marc Glisse
  2011-11-22 18:18 ` Ian Lance Taylor
  1 sibling, 0 replies; 3+ messages in thread
From: Marc Glisse @ 2011-11-22 18:11 UTC (permalink / raw)
  To: Pawel Sikora; +Cc: gcc-help

On Tue, 22 Nov 2011, Pawel Sikora wrote:

> Hi,
>
> afaics the gcc-4.6 accepts following code:
>
> #include <string>
> int main()
> {
>        std::string line( "blabla" );
>        line.erase( --line.end() );
> }
>
> but other compilers reject such code:
>
> msvc8  : error C2105: '--' needs l-value
> comeau : error: expression must be a modifiable lvalue
>
> is it a bug in g++?

Hello,

your code is wrong. Libraries that use pointers as iterators will fail. 
Libraries that use classes are likely to work. There is nothing wrong with 
the fact that libstdc++ accepts this code.

When references on member functions are implemented, it would be possible 
to reject this code, but I am not sure we want to.

-- 
Marc Glisse

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: confusion about string.erase( --string.end() )
  2011-11-22 17:23 confusion about string.erase( --string.end() ) Pawel Sikora
  2011-11-22 18:11 ` Marc Glisse
@ 2011-11-22 18:18 ` Ian Lance Taylor
  1 sibling, 0 replies; 3+ messages in thread
From: Ian Lance Taylor @ 2011-11-22 18:18 UTC (permalink / raw)
  To: Pawel Sikora; +Cc: gcc-help

Pawel Sikora <pluto@agmk.net> writes:

> afaics the gcc-4.6 accepts following code:
>
> #include <string>
> int main()
> {
>         std::string line( "blabla" );
>         line.erase( --line.end() );
> }
>
> but other compilers reject such code:
>
> msvc8  : error C2105: '--' needs l-value
> comeau : error: expression must be a modifiable lvalue
>
> is it a bug in g++?

I don't think this is a bug.  std::string::end is required to return a
std::string::iterator.  The type of std::string::iterator is
implementation defined.  It so happens that the g++ implementation of
std::string::iterator has an operator-- method.  Apparently this is not
true of the implementations used by MSVC or Comeau.  So it's not a bug,
it's just an implementation difference permitted by the standard.

Ian

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-11-22 17:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-11-22 17:23 confusion about string.erase( --string.end() ) Pawel Sikora
2011-11-22 18:11 ` Marc Glisse
2011-11-22 18:18 ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).