Question about postfix ++ precedence

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mrpermuted@gmail.com

    #1

    Question about postfix ++ precedence

    Hello,

    Could someone explain to me why the postfix ++ operator is listed near
    the top of the operator precedence (above even prefix ++ on my book's
    chart) but is not evaluated before operators of lower precedence? It
    seems like a contradiction to me.

    Thanks,
    Glen

  • Victor Bazarov

    #2
    Re: Question about postfix ++ precedence

    mrpermuted@gmai l.com wrote:[color=blue]
    > Could someone explain to me why the postfix ++ operator is listed near
    > the top of the operator precedence (above even prefix ++ on my book's
    > chart) but is not evaluated before operators of lower precedence? It
    > seems like a contradiction to me.[/color]

    Precedence controls the order of application. Postfix has side effect
    (changing the value of the operand) which is only observable at the
    next sequence point. The result of the postfix increment or decrement
    is the value of the operand _before_ it's applied. No contradiction.

    V
    --
    Please remove capital As from my address when replying by mail


    Comment

    • Old Wolf

      #3
      Re: Question about postfix ++ precedence

      mrpermuted@gmai l.com wrote:
      [color=blue]
      > Could someone explain to me why the postfix ++ operator is listed near
      > the top of the operator precedence (above even prefix ++ on my book's
      > chart) but is not evaluated before operators of lower precedence? It
      > seems like a contradiction to me.[/color]

      Because precedence and order-of-evaluation are unrelated.

      Comment

      • mrpermuted@gmail.com

        #4
        Re: Question about postfix ++ precedence

        > Because precedence and order-of-evaluation are unrelated.

        Ok... what's the difference, then?

        Glen

        Comment

        • Victor Bazarov

          #5
          Re: Question about postfix ++ precedence

          mrpermuted@gmai l.com wrote:[color=blue][color=green]
          >> Because precedence and order-of-evaluation are unrelated.[/color]
          >
          > Ok... what's the difference, then?
          >
          > Glen[/color]

          If you don't concern yourself with such things like overflow,
          precedence determines the logic, while order of execution is
          at the liberty of the optimizer.

          unsigned a, b, c; // no overflow
          ...
          unsigned d = a + b - c;

          '+' and '-' have the same precedence and while they are usually
          grouped from left to right, the compiler is free to evaluate the
          above expression as if it were written

          unsigned d = a + (b - c);

          or

          unsigned d = (a - c) + b;

          In that context we say that the order of execution is unspecified.

          That's how I see it anyway. A proper book on on computer science
          must explain it better.

          V
          --
          Please remove capital As from my address when replying by mail


          Comment

          • Zara

            #6
            Re: Question about postfix ++ precedence

            On 6 Feb 2006 17:21:02 -0800, mrpermuted@gmai l.com wrote:
            [color=blue]
            >Hello,
            >
            >Could someone explain to me why the postfix ++ operator is listed near
            >the top of the operator precedence (above even prefix ++ on my book's
            >chart) but is not evaluated before operators of lower precedence? It
            >seems like a contradiction to me.
            >
            >Thanks,
            >Glen[/color]

            With an example:

            a=b+c++;

            Operator precedence tells the compiler to interpret that line as

            a= b+ (c++); // YES

            and not something like

            a=(b+c)++; // NO

            But order of evaluation is unspecified. The expression could be
            evaluated as:

            Either:
            -- calculate b+c
            -- increment c
            -- store calculation in a

            Or:

            -- calculate b+c
            -- store calculation in a
            -- increment c

            whichever fits better the compiler, depending on lots of things, such
            as optimization level, surrounding operations... Same compiler may
            compile the expression in many different ways, as long as the
            calculation is correct.

            Regards,

            Zara

            Comment

            Working...