I
Ioannis Vranos
I do not remember if the following is undefined behaviour:
somefunc(i++);
Is it?
Thanks.
somefunc(i++);
Is it?
Thanks.
it depends on somefunc and i.Ioannis said:I do not remember if the following is undefined behaviour:
somefunc(i++);
Is it?
Thanks.
I do not remember if the following is undefined behaviour:
somefunc(i++);
Is it?
Ioannis said:I do not remember if the following is undefined behaviour:
somefunc(i++);
Is it?
Thanks.
No. It is not undefined behavior.
The function "somefunc()" receives the original value of i as
its argument, and i is incremented afterwards. The incremented
value of i is not seen by the function called, only by the
caller.
[...]Ioannis said:somefunc(i++);
I think this may be UB:
void somefunc(sometype &v) {}
Right, but sometype may not follow proper semantics for operator++, soSG said:[...]Ioannis said:somefunc(i++);
I think this may be UB:
void somefunc(sometype &v) {}
Assuming "i++" returns an rvalue it won't compile.
This is the case for sometype=int.
Cheers!
SG
That is indeed UB.Ioannis said:I think this is undefined behaviour though:
void somefunc(int x, int y);
somefunc(++i, i);
Daniel Pitts said:That is indeed UB.
the order that the arguments are evaluated is undefined.
int i = 0;
somefunc(++i, i) might call somefunc(0,0), or somefunc(0,1).
That is indeed UB.
the order that the arguments are evaluated is undefined.
int i = 0;
somefunc(++i, i) might call somefunc(0,0), or somefunc(0,1).
By this reason, it does not have to invoke undefined
behavior, it also might invoke merely »unspecifed behavior«.
I know »f(++i,++i)« to invoke undefined behavior, but here
an object is attempted to be /modified twice/ between two
sequence points. This is not the case for »f(++i,i)«.
ISO/IEC 14882:2003(E) says:
(0) »Between the previous and next sequence point a scalar
object shall have its stored value modified at most once
by the evaluation of an expression.«
(1) »Furthermore, the prior value shall be accessed only to
determine the value to be stored.«
ISO/IEC 14882:2003(E), 5#4
(0) does not apply to »f(++i,i)«.
(1) seems somewhat cryptic to me. For example, it seems to
depend on the implementation. If the second »i« in
»f(++i,i)« does refer to the prior value (which might happen
in some C++ implementations), then (1) does apply. But in
some implementations it might refer to the post value. Then,
(1) would not apply?
Also, in »printf( "%d", i )« the value of i is /not/
accessed to determine a »value to be stored« somewhere, but
it is accessed between two sequence points. So, does (1)
forbid this?
There also is this example:
»i = ++i + 1; // the behavior is unspecified«
ISO/IEC 14882:2003(E), 5#4
(Saying that this is unspecified does not exclude that it is
also undefined. But is it?)
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.