Thread: string::size_type incrementation

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    string::size_type incrementation

    Hi all,

    I'm having problems with the following code:

    Code:
    std::string somestring;
    std::string::size_type st;
    st++ = somestring.rfind(' ');
    I'm using g++ 4.0.3 and it gives me an error about, "invalid lvalue in assignment."

    Obviously, I could modify the last line like this and just get along with it.
    Code:
    st = somestring.rfind(' ') + 1;
    So I've learned for the first time that you cannot increment lvalues (if that's what's causing the problem).

    So could somebody tell me why this restriction exists? Or at least throw me a link? Hopefully something more detailed than, "the standard says so."

    At the moment, I really can't imagine of what use such a restriction might be.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I believe the result of the ++ operator is not a modifiable lvalue.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Stupid me. I always assumed the assignment operator had precedence over ++. Turns out its the other way around.

    Clear as mud.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. incrementation of "Homework" threads
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-06-2004, 09:08 PM