Reputation: 20756
ISO/IEC 9899:1999
7.13.1.1 The setjmp macro
Environmental limits 4 An invocation of the setjmp macro shall appear only in one of the following contexts: — the entire controlling expression of a selection or iteration statement; — one operand of a relational or equality operator with the other operand an integer constant expression, with the resulting expression being the entire controlling expression of a selection or iteration statement; — the operand of a unary ! operator with the resulting expression being the entire controlling expression of a selection or iteration statement; or — the entire expression of an expression statement (possibly cast to void).
So, the only variants of using setjmp are the following:
if (setjmp(buf))
while (setjmp(buf))
for (;; setjmp(buf))
if (setjmp(buf) == 0)
while (setjmp(buf) == 0)
for (;; setjmp(buf) == 0)
if (!setjmp(buf))
while (!setjmp(buf))
for (;; !setjmp(buf))
setjmp(buf);
(void)setjmp(buf);
And we can't use this statements:
int foo = setjmp(buf);
foo = setjmp(buf);
Right? What they mean by the iteration statement? The last statement of the for loop?
Upvotes: 4
Views: 195
Reputation: 78943
No you can't use
int foo = setjmp(buf);
foo = setjmp(buf);
The reason for the later (assignment) is probably that an assignment is an expression, that can have more than just an identifier on the left side. If the left side is an lvalue expression the standard imposes no order in which the subexpressions are evalutated. So if you have
int* f(void);
*f() = setjmp(buf);
*f()
and setjmp(buf)
could be evaluated in any order. Since setjmp
makes a snapshot of the actual state of the abstract state machine, the semantics of boths orders would be completely different.
For the first line (initialization) this problem doesn't occur, I think. So I guess this could be added as a valid use. But it would have to be discussed carefully if there are no border cases that still require an evaluation on the left side.
(Eric already replied for the selection statements.)
Upvotes: 4
Reputation: 223254
Selection statements are if
(including if…else
) and switch
. Iteration statements are while
, do…while
, and for
.
Upvotes: 2