ブロック構文ローカルな変数宣言のへんなところ
int main(int, char**) { for (static int a = 3; --a;) {} // これは通る for (int a = 2; int b = --a; ) {} // キモすぎるがこれも通る if (bool b = true) {} // これも通る struct foo { foo(int) {} operator bool() { return false; } }; if (struct foo f = 2) {} // これは通る if (struct foo f( 2 )) {} // これはgcc4.3では通らない if (static bool b = true) {} // これは通らない
仕様でそうなってるわけだけど。
まずは if と switch の場合。
selection-statement: if ( condition ) statement if ( condition ) statement else statement switch ( condition ) statement condition: expression type-specifier-seq attribute-specifier? declarator = initializer-clause type-specifier-seq attribute-specifier? declarator braced-init-list
while
while (T t = x) statement is equivalent to label: { // start of condition scope T t = x; if (t) { statement goto label; } } // end of condition scope
そして for。
for ( for-init-statement; condition? ; expression? ) statement is equivalent to { for-init-statement while ( condition ) { statement expression ; } }
うーん。