wsdwsd skrev 2013-12-21 13:22:
> I have some problems to ask you. Can I understand i++ in that order? I
> know compiler needn't do as that. I don't know whether C++ std have
> asked compilers do the same things? I mean whether I can understand them
> in that order without errors.
> And do you think
> stack.emplace(stack.top()++);
> could be written in my codes, for I couldn't do such a thing in a
> different way easily?
You could write stack.emplace(stack.top()++); , and it is guaranteed to
work. All parameters are fully evaluated, including their side effects,
before a function is called.
Bo Persson
> ------------------------------------------------------------------------
> From: Billy O'Neal <mailto:
billy...@gmail.com>
> Sent: 2013/12/21 14:24
> To: std-proposals <mailto:
std-pr...@isocpp.org>
> Subject: Re: [std-proposals] Some questions about ++ operator
>
> 1. ++++s is ill formed; ++ can only be applied to an lvalue. ++s = 10 is
> undefined behavior because the write to s as part of ++ and the write
> assigning s to 10 are unsequenced. For instance, a compiler could add 1
> to s and store the result in a register, then assign 10 to s, then
> assign the register contents of the register to s. (Result: s = s + 1)
> The compiler could also add 1 to s, store the result back to s, and then
> assign 10 to s. (Result: s == 10). (Of course, undefined behavior means
> more than this, but these are the most likely actual behaviors by an
> actual compiler)
> 2. No. That is semantically what occurs, but a compiler is under no
> obligation to actually do the operations in that order. (If it were done
> as separate statements, the compiler would have no freedom to reorder here)
> 3. stack.emplace(stack.top()++); is well defined because the evaluation
> of function arguments is sequenced before the execution of a function.
> Therefore, the ++ must occur before the emplacement (at least as far as
> observable behavior of the program is concerned). See N3691 1.9
> [intro.execution]/15:
>
>
> When calling a function (whether or not the function is inline),
> every value computation and side effect
> associated with any argument expression, or with the postfix
> expression designating the called function, is
> sequenced before execution of every expression or statement in the
> body of the called function.
>
>
>
>