If the typeof expression sizeof is not a modified array type, then the expression is not evaluated because the type is fully known at compile time. int has no modified parts.
In C ++ (until at least C ++ 11) there are no changed types (at least not like in the C concept), you can argue that new int[a++] uses a variable array type, but the type do not run to any other part of the language, in particular, not to sizeof ), therefore, in C ++, the expression sizeof is never evaluated. C does not indicate whether an expression is evaluated if it does not affect the size of the modified array type. for example
int main() { int a = 10; int b = sizeof(int[a++ ? 1 : 1]); cout<<"a: "<<a<<endl; cout<<"b: "<<b<<endl; return 0; }
In C (starting with C99), this can output 11 for a , but it can also output 10 , depending on whether the compiler is sufficiently equipped to omit the a++ score, deducing that sizeof int[10] computed at compile time.
Footnote: mutable array types are also called VLA types (variable length arrays). In short, a modified type with a variable type is a type that is either a VLA type or a type that depends on one. For example int(*)[a++] .
Johannes Schaub - litb
source share