Mixing post-(inc/dec)rement on the same variable

Discussion in 'C++' started by Ravi, Jul 19, 2010.

  1. Ravi

    Ravi Guest

    In C++ (and also in C), if I write:

    ++x--
    ++(x--)

    i get the error: lvalue required as increment operand

    However (++x)-- compiles. I am confused.
     
    Ravi, Jul 19, 2010
    #1
    1. Advertising

  2. * Ravi, on 19.07.2010 15:51:
    > In C++ (and also in C), if I write:
    >
    > ++x--
    > ++(x--)
    >
    > i get the error: lvalue required as increment operand


    Which means at least your compiler parses the first as if it were the second.

    Not sure what the standards say, because it does not matter.

    For you should never modify a variable twice between sequence points, that's
    Undefined Behavior.


    > However (++x)-- compiles. I am confused.


    No problem, just Don't Do It. :)

    Technically, in C++98 prefix '++x' produces 'x' as an lvalue, i.e. a reference,
    while 'x--' produces the original value as an rvalue, like 42; you can't change 42.

    But the technical explanation doesn't matter because also '(++x)--' is UB, even
    if it compiles.


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Jul 19, 2010
    #2
    1. Advertising

  3. Ravi

    SG Guest

    On 19 Jul., 16:06, Alf P. Steinbach wrote:
    > * Ravi, on 19.07.2010 15:51:
    >
    > > In C++ (and also in C), if I write:

    >
    > > ++x--
    > > ++(x--)

    >
    > > i get the error: lvalue required as increment operand

    >
    > Which means at least your compiler parses the first as if it were the second.
    > Not sure what the standards say, because it does not matter.


    It does matter. Perhaps not with built-ins like int but for class-
    types with overloaded operators. Postfix operators always bind
    stronger than prefix operators. This is a simple rule one should keep
    in mind. Another example: &x[5] is equivalent to &(x[5]) and not (&x)
    [5].

    Cheers!
    SG
     
    SG, Jul 19, 2010
    #3
  4. Ravi

    cpp4ever Guest

    On 07/19/2010 02:51 PM, Ravi wrote:
    > In C++ (and also in C), if I write:
    >
    > ++x--
    > ++(x--)
    >
    > i get the error: lvalue required as increment operand
    >
    > However (++x)-- compiles. I am confused.


    Postfix operator returns a value and not an lvalue, whereas the prefix
    operator will return an lvalue,for which the operator requires.

    A word of warning with regard to these prefix/postfix operators, never
    use them on a variable within an expression, if that variable appears
    more than once in the expression.

    // BAD b is used twice in left-hand expression
    r = ((b++) + sqrt(a*a - 4*a*b))/(2*a);

    // EVEN WORSE as the operator is used twice on b in one expression
    r = (++b) + (++b);

    Unfortunately, how these actually behave between sequence points is
    compiler dependent, hence for the second example with b=3, will the
    result be 7 or 8? Preincrement done during expression evaluation, or
    before the expression evaluation, and this is compiler dependent.

    IMHO it's better not to use them as part of any expression calculation.

    HTH

    cpp4ever
     
    cpp4ever, Jul 19, 2010
    #4
  5. * SG, on 19.07.2010 16:23:
    > On 19 Jul., 16:06, Alf P. Steinbach wrote:
    >> * Ravi, on 19.07.2010 15:51:
    >>
    >>> In C++ (and also in C), if I write:

    >>
    >>> ++x--
    >>> ++(x--)

    >>
    >>> i get the error: lvalue required as increment operand

    >>
    >> Which means at least your compiler parses the first as if it were the second.
    >> Not sure what the standards say, because it does not matter.

    >
    > It does matter. Perhaps not with built-ins like int but for class-
    > types with overloaded operators. Postfix operators always bind
    > stronger than prefix operators. This is a simple rule one should keep
    > in mind. Another example:&x[5] is equivalent to&(x[5]) and not (&x)
    > [5].


    Well, when u phrase it that way... :)

    I've probably encountered that summary rule before & forgotten it.

    It's a good one.


    Cheers,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Jul 19, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sebestyén Gábor

    post inc problem

    Sebestyén Gábor, Mar 3, 2005, in forum: Ruby
    Replies:
    20
    Views:
    311
    Malte Milatz
    Mar 5, 2005
  2. J.E./C.Y.Cripps
    Replies:
    0
    Views:
    128
    J.E./C.Y.Cripps
    Dec 3, 2004
  3. kellinorman
    Replies:
    7
    Views:
    123
    Bart Lateur
    Apr 19, 2006
  4. Replies:
    3
    Views:
    1,642
  5. Michael Yang
    Replies:
    9
    Views:
    210
    c.cobb
    May 9, 2007
Loading...

Share This Page