I think it should be 01 but someone says its "undefined", any reason for that?
c++
is both an increment and an assignment. When the assignment occurs (before or after other code on that line) is left up to the discretion of the compiler. It can occur after the cout <<
or before.
This can be found in the C99 standard http://www.open-std.org/JTC1/SC22/wg14/www/docs/n1124.pdf
You can find it on page 28 in the pdf or section 5.1.2.3
the actual increment of p can occur at any time between the previous sequence point and the next sequence point
Since someone asked for the C++ standard (as this is a C++ question) it can be found in section 1.9.15 page 10 (or 24 in pdf format)
evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced
It also includes the following code block:
i = v[i++]; // the behavior is undefined
i = 7, i++, i++; // i becomes 9
i = i++ + 1; // the behavior is undefined
I feel that the C99 standard's explanation is clearer, but it is true in both languages.
c++
and c
. –
Silberman It is undefined behavior if you modify a value and then read it (or try to modify it again) without an intervening sequence point. The concept of a sequence point in C++ is a bit technical (you can read a little about it here), but the bottom line is that stream insertion (<<
) is not a sequence point.
The reason why this is undefined behavior is because, in the absence of a sequence point, the compiler is allowed to re-order operations in any way it sees fit. That is, it is permitted to retrieve the value of c
(and hold onto it for the second insertion) and then afterwords execute c++
to get the value for the first insertion. So you can't be sure whether the increment will occur before or after the value of c
for the second insertion is determined.
The behavior is defined but unspecified. The relative order of evaluating the two uses of 'c' in the expression isn't specified. However, if you convert it to functional notation, it looks like this:
cout.operator<<(c++).operator<<(c);
There's a sequence point between evaluating the arguments to a function, and executing the body of the function, and function bodies aren't interleaved, so the result is only unspecified, not undefined behavior.
If you didn't have an overloaded operator:
int c=0;
int a = c++ << c;
Then the behavior would be undefined, because both modifying and using the value of c
without an intervening sequence point.
Edit: The sequence litb
brings up is simply wrong. The standard specifies (§1.9/17): "When calling a function (whether or not the function is inline), there is a sequence point after the evaluation of all function arguments (if any) which takes place before execution of any expressions or statements in the function body."
This clearly written with the idea that arguments are evaluated, then (immediately afterward) the body of the function is executed. The sequence he suggests, in which arguments to one function are evaluated, then arguments to another, then execution of both function bodies doesn't seem to have been intended, but also isn't prohibited. That, however, changes nothing -- the requirement is still that: "...there is a sequence point after the evaluation of all function arguments (if any)..."
The subsequent language about execution of the body does NOT remove the requirement for a sequence point after evaluating all function arguments. All other evaluation, whether of the function body or other function arguments follows that sequence point. I can be as pedantic and perverse as anybody about mis-reading what's clearly intended (but not quite stated) -- but I can't imagine how "there is a sequence point after the evaluation of all function arguments" can be read as meaning "there is NOT a sequence point after the evaluation of all function arguments."
Neil's point is, of course, correct: the syntax I've used above is for member functions. For a non-member overload, the syntax would be more like:
operator<<(operator<<(cout,c++), c);
This doesn't remove the requirement for sequence points either though.
As far as it being unspecified: it's pretty simple really: there's a sequence point after evaluating all function arguments, so all the arguments for one function call must be fully evaluated (including all side effects), then arguments for the other function call can be evaluated (taking into account any side effects from the other) -- BUT there's no requirement about WHICH function call's arguments must be evaluated first or second, so it could be c
, then c++
, or it could be c++
, then c
-- but it has to be one or the other, not an interleaving.
c++
and c
without an intervening sequence point is fully conforming. –
Exteriorize c
and c++
both be evaluated before the sequence point for the first function call? In this case there would be no intervening sequence point and undefined behaviour. –
Diminution op<<
is a member function? –
Diminution [A = eval(c++), B = eval(c)] # [operator<<(A)] # [operator<<(B)]
(those hashes shall signify that there is a sequence point). –
Exteriorize operator << (int)
is a member function of basic_ostream
(see the original C++ standard, section 27.6.2.1), so it does apply here. However, I believe (but am not certain that) Johannes is correct. –
Wield c
, in which case it's defined behavior, or there isn't, and it's undefined behavior. I need to do some more careful reading about sequence points. –
Wield op<<
. Evaluating right to left (as one option), I start by evaluating c
, then move on to op<<(cout, c++)
. OK, so I haven't reached either function sequence point yet as I move on to the parameters of op<<(cout, c++)
. Right to left, I evaluate c++
but I still haven't reached either sequence point implied by the function calls. Surely this is UB? –
Diminution c++
happening before the first op<<, and there was a sequence point after c
also happening before the second op<<. So yes, i agree. Also i don't think it's nonsense. Here are the semantics: 1 = <evaluation of args of 1st "op<<">, N = <sequence point>, 100 = <evaluation of function body>. Then, you say "ah, N must be 2", but i say "N can be 50, and the numbers in between can represent other evaluations in that expression, because 50 is less than 100 and greater than 1". –
Exteriorize op<<(cout,c++)
not the first argument in the 'outer' op<<
? The fact that it evaluates to cout
(via and lvalue reference return value) - if indeed it does - is an implementation detail. You seem to allow that c
can be evaluated before c++
but there are only two sequence points for the functions and I can't see how either of them (in this evaluation order) can occur between the evaluation of c
and c++
. –
Diminution f( g( a, b ), c )
the standard states that evaluation of a
and b
must happen before g
is evaluated, and that evaluation of both g
and c
must occur before evaluation of f
. But that is only a partial order. The evaluation order can be: c
, b
, a
, g
, f
, or b
, c
, a
, g
, f
, or b
, a
, g
, ... you follow. Now, if you replace f == g == operator<<
, a == cout
, b == c++
, c == c
(last one was easy!) there are different combinations that allow c++
to be executed before or after c
–
Paleozoic b
and c
? –
Diminution f( g(a, b), c, d )
the order can be a d b # g # c # f #
. Of course, this time the sequence point is quite "immediately afterwards" evaluating g's arguments. But as can be seen in this example, this doesn't quite forbid interleaving evaluations of other subexpressions. –
Exteriorize The reason it is undefined is that the compiler is free to calculate function parameters in any order. Consider if you where calling a function (because you are, but it's easier to envision when it's in function syntax):
cout.output(c++).output(c);
The compiler may hit the parameters in reverse order, forward order, or whatever. It may call the first output before calculating the parameter to the second output or it may do both and then call.
As I see it, f(c++); is equivalent to: f(c); c += 1;
And f(c++,c++); is equivalent to: f(c,c); c += 1; c += 1;
But it may be the case that f(c++,c++); becomes f(c,c+1); c+= 2;
An experiment with gcc and clang, first in C
#include <stdio.h>
void f(int a, int b) {
printf("%d %d\n",a,b);
}
int main(int argc, char **argv) {
int c = 0;
f(c++,c++);
return 0;
}
and in C++
#include <iostream>
int main(int argc, char **argv) {
int c = 0;
std::cout << c++ << " " << c++ << std::endl;
return 0;
}
Is interesting, as gcc and g++ compiled results in 1 0 whereas clang compiled results in 0 1
© 2022 - 2024 — McMap. All rights reserved.