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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With