rahnema1 rahnema1 - 1 month ago 5x
C++ Question

static_assert in for-init-statement of the for statement

What prevents compilers from compiling a

placed in for-init-statement of the for statement?

For example:

for(static_assert(true, ""); false; ); // error


Grammatically. A for loop is:

for ( init-statement conditionopt ; expressionopt ) statement

where an init-statement is either an expression followed by a ; or a simple-declaration. A static_assert-declaration is neither of those things, hence it can't go there.