Re: Post increment ++ has higher precedence than pre increment ++. Why?

Discussion in 'C++' started by Alf P. Steinbach /Usenet, May 22, 2011.

  1. * Prasoon Saurav, on 22.05.2011 11:40:
    > Why does post increment ++ has higher precedence than pre increment +
    > +?
    >
    > This question appeared at stackoverflow.com :
    > http://stackoverflow.com/questions/...r-have-higher-precedence-than-prefix-operator
    >
    > One of the answers mentioned that this was because the designers of
    > the language decided so.
    > My question is why did the language designers (people who designed the
    > grammar of the language) decide to do so?


    First, the C++ standard does not define an operator precedence, and even before
    the standardization there was never a defined precedence.

    Instead, the effective operator precedence that you see, and that is documented
    in many books, and which is not a perfect one!, results from the grammar.

    So the question is, why is the C++ grammer defined so that

    ++o++

    parses as

    ++(o++) // A

    rather than

    (++o)++ // B

    ?

    Well, parse A yields an error at compilation time (you cannot increment an
    rvalue), while parse B yields Undefined Behavior at run time.

    Which would you rather have?


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, May 22, 2011
    #1
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,993
    Smokey Grindel
    Dec 2, 2006
  2. Replies:
    104
    Views:
    11,029
    Jordan Abel
    Oct 28, 2005
  3. Replies:
    99
    Views:
    2,511
    eliza81
    Jun 11, 2010
  4. trans.  (T. Onoma)

    to_s precedence higher than -@ for Infinity ?

    trans. (T. Onoma), Oct 11, 2004, in forum: Ruby
    Replies:
    4
    Views:
    99
    Markus
    Oct 11, 2004
  5. Peng Yu

    post increment or pre increment?

    Peng Yu, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    530
    Peter J. Holzer
    Nov 23, 2008
Loading...

Share This Page