Reputation: 11433
In GNUC C, you can use typeof(expression)
, and it is legal to use an expression with side effects inside. So for example you can have this C code:
int x = 0;
typeof(x++) y;
In this case, the side effect is ignored and x is still zero afterwards (this makes sense as types are a compile-time-only thing).
However, the GCC documentation says:
The operand of typeof is evaluated for its side effects if and only if it is an expression of variably modified type or the name of such a type.
What does this sentence mean? Is it really possible to write typeof
with a side effect and have the side effect actually be executed at runtime? For me, this sentence seems to indicate this.
Upvotes: 8
Views: 483
Reputation: 11433
Yes, it is possible in certain cases to have side-effects inside typeof
evaluated. As the documentation says, it needs to be within a "variably modified type". This is a type which depends on some runtime value, such as int[x]
.
So the following code is legal, declares y to be of type int[1]
, and x has the value 1 afterwards:
int x = 0;
typeof(int[++x]) y;
Of course, equally to variably modified types in general, this is only legal for local variable declarations (inside function).
Upvotes: 9