Is preincrementing the same variable twice valid c++?

Discussion in 'C++' started by Eric Lilja, Mar 1, 2007.

  1. Eric Lilja

    Eric Lilja Guest

    ...or does it violate the rule that a variable may not be modified more
    than once between two sequence points?

    int n = 0;
    std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;

    - Eric
    Eric Lilja, Mar 1, 2007
    #1
    1. Advertising

  2. Eric Lilja wrote:
    > ..or does it violate the rule that a variable may not be modified more
    > than once between two sequence points?
    >
    > int n = 0;
    > std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;


    It's undefined.
    Gianni Mariani, Mar 1, 2007
    #2
    1. Advertising

  3. Eric Lilja

    Pete Becker Guest

    Eric Lilja wrote:
    > ..or does it violate the rule that a variable may not be modified more
    > than once between two sequence points?
    >


    There's a sequence point before each function call (i.e. before each <<
    function), so that rule doesn't apply. But the order of evaluation of
    arguments is unspecified, so you can't count on any particular order for
    the various values of n.

    > int n = 0;
    > std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;
    >
    > - Eric
    >



    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
    Pete Becker, Mar 1, 2007
    #3
  4. On Mar 1, 1:30 pm, Pete Becker <> wrote:
    > Eric Lilja wrote:
    > > ..or does it violate the rule that a variable may not be modified more
    > > than once between two sequence points?

    >
    > There's a sequence point before each function call (i.e. before each <<
    > function), so that rule doesn't apply. But the order of evaluation of
    > arguments is unspecified, so you can't count on any particular order for
    > the various values of n.


    That's not true

    > > int n = 0;
    > > std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;

    ^^^^^^^^^^^^^^^^^
    This has to execute before the << ' ' can, otherwise there won't be a
    ostream as first parameter to the << ' '-call.

    --
    Erik Wikström


    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Mar 1, 2007
    #4
  5. * Erik Wikström:
    > On Mar 1, 1:30 pm, Pete Becker <> wrote:
    >> Eric Lilja wrote:
    >>> ..or does it violate the rule that a variable may not be modified more
    >>> than once between two sequence points?

    >> There's a sequence point before each function call (i.e. before each <<
    >> function), so that rule doesn't apply. But the order of evaluation of
    >> arguments is unspecified, so you can't count on any particular order for
    >> the various values of n.

    >
    > That's not true


    What isn't true?


    >>> int n = 0;
    >>> std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;

    > ^^^^^^^^^^^^^^^^^
    > This has to execute before the << ' ' can, otherwise there won't be a
    > ostream as first parameter to the << ' '-call.


    Arguments can be evaluated before anything else happens.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Mar 1, 2007
    #5
  6. Eric Lilja

    Pete Becker Guest

    Erik Wikström wrote:
    > On Mar 1, 1:30 pm, Pete Becker <> wrote:
    >> Eric Lilja wrote:
    >>> ..or does it violate the rule that a variable may not be modified more
    >>> than once between two sequence points?

    >> There's a sequence point before each function call (i.e. before each <<
    >> function), so that rule doesn't apply. But the order of evaluation of
    >> arguments is unspecified, so you can't count on any particular order for
    >> the various values of n.

    >
    > That's not true
    >
    >>> int n = 0;
    >>> std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;

    > ^^^^^^^^^^^^^^^^^
    > This has to execute before the << ' ' can, otherwise there won't be a
    > ostream as first parameter to the << ' '-call.
    >


    The order of the calls to operator << is well defined, but the order of
    evaluation of the arguments is unspecified. The compiler can compute the
    value of the first ++n, then the n, then the final ++n. It can also do
    them in reverse order, or any other order.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
    Pete Becker, Mar 1, 2007
    #6
  7. Eric Lilja

    Ron Natalie Guest

    Pete Becker wrote:
    > Eric Lilja wrote:
    >> ..or does it violate the rule that a variable may not be modified more
    >> than once between two sequence points?
    >>

    >
    > There's a sequence point before each function call (i.e. before each <<
    > function), so that rule doesn't apply. But the order of evaluation of
    > arguments is unspecified, so you can't count on any particular order for
    > the various values of n.
    >

    That rule does apply. Since the function parameters may be evaluated
    in any order, both ++n subexpressions may be executed before ANY
    function call occurs. Hence you will have the undefined behavior or
    twice modifying a value between sequence points. The rule requires
    that be true for ANY ALLOWABLE ORDERING of the expression.
    Ron Natalie, Mar 1, 2007
    #7
  8. Eric Lilja

    Ron Natalie Guest

    Erik Wikström wrote:
    > On Mar 1, 1:30 pm, Pete Becker <> wrote:
    >> Eric Lilja wrote:
    >>> ..or does it violate the rule that a variable may not be modified more
    >>> than once between two sequence points?

    >> There's a sequence point before each function call (i.e. before each <<
    >> function), so that rule doesn't apply. But the order of evaluation of
    >> arguments is unspecified, so you can't count on any particular order for
    >> the various values of n.

    >
    > That's not true


    Partially true.

    >
    >>> int n = 0;
    >>> std::cout << ++n << ' ' << n << ' ' << ++n << std::endl;

    > ^^^^^^^^^^^^^^^^^
    > This has to execute before the << ' ' can, otherwise there won't be a
    > ostream as first parameter to the << ' '-call.
    >


    The real issue is that the compiler is free to execute both ++n
    subexpressions in any order and at any time prior to the call
    of the operator<< where they are parameters. Specifically it
    can execute them BOTH before either operator << call ,causing
    undefined behavior.
    Ron Natalie, Mar 1, 2007
    #8
    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. Lawrance
    Replies:
    0
    Views:
    442
    Lawrance
    Nov 30, 2003
  2. Kiuhnm

    twice(twice(x))

    Kiuhnm, Apr 1, 2006, in forum: C++
    Replies:
    2
    Views:
    384
    Kiuhnm
    Apr 1, 2006
  3. Replies:
    1
    Views:
    1,432
    Victor Bazarov
    Nov 6, 2007
  4. Mug
    Replies:
    4
    Views:
    290
  5. Rajesh.V

    Same user twice in the same aspx..

    Rajesh.V, Aug 6, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    127
    Rajesh.V
    Aug 6, 2003
Loading...

Share This Page