views:

307

answers:

1

Sorry for opening this topic again, but thinking about this topic itself has started giving me an Undefined Behavior. Want to move into the zone of well-defined behavior.

Given

int i = 0;
int v[10];
i = ++i;     //Expr1
i = i++;     //Expr2
++ ++i;      //Expr3
i = v[i++];  //Expr4

I think of the above expressions (in that order) as

operator=(i, operator++(i))    ; //Expr1 equivalent
operator=(i, operator++(i, 0)) ; //Expr2 equivalent
operator++(operator++(i))      ; //Expr3 equivalent
operator=(i, operator[](operator++(i, 0)); //Expr4 equivalent

Now coming to behaviors here are the important quotes from C++ 0x.

$1.9/12- "Evaluation of an expression (or a sub-expression) in general includes both value computations (including determining the identity of an object for lvalue evaluation and fetchinga value previously assigned to an object for rvalue evaluation) and initiation of side effects."

$1.9/15- "If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined."

[ Note: Value computations and side effects associated with different argument expressions are unsequenced. —end note ]

$3.9/9- "Arithmetic types (3.9.1), enumeration types, pointer types, pointer to member types (3.9.2), std::nullptr_t, and cv-qualified versions of these types (3.9.3) are collectively called scalar types."

-In Expr1, the evaluation of the expression 'i' (first argument), is unsequenced with respect to the evaluation of the expession 'operator++(i)' (which has side effect).

Hence Expr1 has undefined behavior

-In Expr2, the evaluation of the expression 'i' (first argument), is unsequenced with respect to the evaluation of the expession 'operator++(i, 0) (which has side effect)'.

Hence Expr2 has undefined behavior

-In Expr3, the evaluation of the lone argument 'operator++(i)' is required to be complete before the outer 'operator++' is called.

Hence Expr3 has well defined behavior

-In Expr4, the evaluation of the expression 'i' (first argument) is unsequenced with respect to the evaluation of the operator[](operator++(i, 0) (which has a side effect).

Hence Expr4 has undefined behavior

Is this understanding correct?

EDIT 2:

The method of analyzing the expressions as in OP is not correct. This is because

As @Potatoswatter notes - "clause 13.6 does not apply. See the disclaimer in 13.6/1, "These candidate functions participate in the operator overload resolution process as described in 13.3.1.2 and are used for no other purpose." They are just dummy declarations; no function-call semantics exist with respect to built-in operators."

+6  A: 
Potatoswatter
Sorry, I mentioned C++0X a little bit late in my post
Chubsdad
@Chubdad: C++0x doesn't have sequence points…
Potatoswatter
@Potato AFAIK it's just the term "sequence point" that's been deprecated in favor of more clear wording, but it's still there.
NullUserException
@NullUser: There's a concept of sequencing, but the C way of saying the machine is either in a fully-determined state or indeterminate is gone.
Potatoswatter
I also thought so. But http://stackoverflow.com/questions/3850040/complicated-expression-involving-logical-and/3850092#3850092 brought in some changes to the thought process. Confusion is now on 'i = ++i;' and I have not been able to get my thinking straight on this one. With this post I am trying to verify if my thought process is right (thinking in terms of operator equivalent).
Chubsdad
@Potatoswatter : All four expressions [invoke UB in C++0x](http://groups.google.com/group/comp.lang.c++/browse_thread/thread/97e9767139bebcc6)
Prasoon Saurav
@Chubsdad: The value of `++i` is an operand to the `=` operator, so at least according to this paragraph, the effects is sequenced. Whether there something else makes it UB, I will check Prasoon's link next.
Potatoswatter
@Potatoswatter: "Native operator expressions are not equivalent to overloaded operator expressions" Is this Standardese? Are you talking here about a difference in sequence point location at the binding of values to function arguments between a built-in/native/default operator= and an overloaded operator= defined as above in user code? Is it possible to give an example of the "native-type case"? So if I have defined an overloaded++ and write i++ it's well defined behavior - otherwise undefined? And this applies to C++98/03 and C++0x?
Peter McGrattan
@Prasoon: No, those unsequenced examples use postincrement. Johannes gives a preincrement example `i = v[++i]` and argues that the side effect is of storing `(i=i+1)` is unsequenced relative to the next, explicit, assignment… that is another argument, and perhaps a good one. But I'm too sleepy to independently evaluate it for now.
Potatoswatter
Potatoswatter
@Potatoswatter : I am not very sure but still think `++ ++i` and `i = ++i` are both UB in C++0x. Read James Kanze's posts [at the end of that discussion]. `;)`
Prasoon Saurav
@Prasoon: Yep, reading Usenet rants is a chore but §1.9/12 and 15 really are pretty clear about it. Updated answer.
Potatoswatter
@Prasoon, the discussion you link to figures that expression 3 does *not* invoke undefined behavior in C++0x according to the draft. Even "James Kanze" agrees to that obvious thing after being told so one-hundred-and-one times (he doubts that "assign" is a side-effect... but if it isn't a modification, *what* is it?). James Kanze oviously does not listen to what one writes. Notice how I write in the middle of that discussion how "sequenced-before" is a transitive relation. He just ignores that, and finally when someone else mentions that, he says "Ohhh, you may have a point there, captain!".
Johannes Schaub - litb
The proof for the well-definedness of Expr3 and for undefinedness of Expr3 can be found here: http://stackoverflow.com/questions/3690141/multiple-preincrement-operations-on-a-variable-in-cc/3691469#3691469
Johannes Schaub - litb
@Johannes: I think you mean undefinedness of Expr1… anyway I don't see how you figure the side effect of the first `++` in Expr3 is sequenced differently from the `=` in Expr1. The two `++` operations generate two distinct assignments to two different values and 1.9/15 does not sequence them.
Potatoswatter
@Potatoswatter yes, i meant undefinedness of Expr1, sorry :) Have to head to work. In the meantime, look into the paragraph for assignment in 5.x, where it's all sequenced. Seeya :)
Johannes Schaub - litb
we are back full circle. Why is 1 having well-defined behavior? That's what was proved to be wrong in the usenet discussion isn't it?
Chubsdad
@Chubdad: because the `++i` is a subexpression equivalent to an assignment expression (5.3.2/1) whose side-effect is ordered "before the value computation of the assignment expression." (5.17/1)
Potatoswatter
That's what I also felt initially. But the usenet discussion forum seems to conclude that behavior of expr1 is ill-formed. @litb also argues for that
Chubsdad
@Potatoswatter like @Chubsdad says, 1 is definitely undefined behavior. Yes, the side effect of `++i` is ordered before value computation of `++i`. This sequences the increment side effect before the "real" assignment side effect to `i`. But we also have a value computation of `i` on the left side in `i = ++i`, which is not sequenced relative to value computation of the right side. This is what makes it undefined. Note that "value computation" can not only mean "read a value" but means "compute what object an lvalue refers to" for glvalue evaluation. See the stackoverflow link above.
Johannes Schaub - litb
Bah, sorry for the churn, old on…
Potatoswatter
@Potatoswatter I see what you mean now.
Johannes Schaub - litb
@Johannes: sorry, my connection went down and I went away forgetting you were waiting… posted the update.
Potatoswatter
@Potatoswatter I removed my answer because you elaborated on yours to give a reasonable rationale.
Johannes Schaub - litb
@Johannes: I guess it's your call, I thought it was a good answer. Do you really think the working paper is still in that much flux? Do you think a DR has been submitted, and would you want to do so? Alisdair hasn't replied to my last few mails, so I think I might have annoyed him ;v) .
Potatoswatter
@Potatoswatter: j = ++ i; should also be undefined behavior. j is just an alias for 'i' isn't it.? I (captial I) fail to understand this and continue to exhibit undefined behavior...:)
Chubsdad
@Chubsdad: Even though it's an alias, its glvalue evaluation does not require a glvalue evaluation of `i`. Generally speaking, evaluating a reference does not require the original object to be on hand. There's no reason it should be UB, so it makes sense there should be an easy loophole or transformation to code which is not UB.
Potatoswatter
its glvalue evaluation does not require a glvalue evaluation of i. Generally speaking, evaluating a reference does not require the original object to be on hand : I really doubt this. Do you have any reference for this 'reference' :)
Chubsdad
@Chubsdad: Consider the function `f( int } … f( i, i );` Still think so? A reference is an alias because its glvalue evaluates identically, not because it syntactically refers to the original object.
Potatoswatter
@Potatoswatter: My understanding is 'j' is an alias for 'i' and even though 'i' is not in scope, 'j' can be used as long as 'i' remains a valid object it originally was. I am not so sure if I understand 'glvalue evaluates identically' vs 'glvalue is the same'.
Chubsdad
This is with reference to 5.5 - "If an expression initially has the type “reference to T” (8.3.2, 8.5.3), the type is adjusted to T prior to any further analysis. `The expression designates the object or function denoted by the reference`, and the expression is an lvalue or an xvalue, depending on the expression.
Chubsdad
@Chubsdad: It designates it by having an identical lvalue; that's the long and the short of it. Lvalue-to-rvalue conversion implements references having the value of the referent object. The reference doesn't tell the compiler to go look at the referenced variable and get its lvalue, because it might not know what variable is referenced. The compiler computes the lvalue of the reference and that lvalue identifies an object. If you want to debate this further, please open a new question.
Potatoswatter
@Potatoswatter: Your wish is my command :) (http://stackoverflow.com/questions/3870172/evaluation-of-a-reference-expression)
Chubsdad
I got it. But my doubt remains. Let's take 'i = ++i'. As per 13.6/18 it can be treated as 'operator=(i, operator++(i))'. The side effect on the scalar object ('i' due to the 2nd argument) is unsequenced relative to value computation of the same scalar object ('i' for the 1st argument). Hence the behavior should be undefined. Can you tell me why it should be well-defined (as you mentioned in your post) from this perspective of thought?
Chubsdad
@Chubsdad: By the standard, it is undefined, as Johannes explained. (I wish he hadn't deleted his answer.) However, it's next to impossible for a compiler to produce any behavior besides the desired, because the supposedly dependent value is a constant. Again, clause 13.6 does not apply. See the disclaimer in 13.6/1, "These candidate functions participate in the operator overload resolution process as described in 13.3.1.2 and are used for no other purpose." They are just dummy declarations; no function-call semantics exist with respect to built-in operators.
Potatoswatter
@Potatoswatter: So do you want to change your post as it says that Expr1 is well-formed?
Chubsdad
@Chubsdad: No, the last edit is spent entirely on discussion of Expr1, so the issue is clearly stated. I'm a bit tired of this.
Potatoswatter
@Chubsdad I think what @Potatoswatter refers to is that the Std says "using the value of the same scalar object". The value of an object is not used if you simply use an lvalue that refers to the object. You have to actually read the value, despite the weird term "value computation". But the term "value" in "value computation" does not seem to refer to the value of an object, but to the "value" of an expression. I.e the glvalue or prvalue result of an expression. But I really think the Standard should be clearer on this issue.
Johannes Schaub - litb
@Johannes Schaub - litb: Oh. Now I understand better. It was little too cryptic for me. I think I am starting to get a hang of the core issue but not there yet.
Chubsdad
I have found two DRs that support this answer: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#637 and http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#222
Johannes Schaub - litb