rahnema1 rahnema1 - 11 months ago 49
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

Answer Source

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.