Is there any way to slip a _Static_assert into an expression in ISO C11?
As @dyp mentioned in the comments, you can abuse the comma operator and a lambda-expression :
([]{static_assert(true,"");}, 42)
Live on Coliru
static_assert
is not an expression (unlike sizeof
), so you can't use it where an expression would be required.
It's not even an expression with type void
(interestingly, throw
is an expression of type void
) so you can't even use it in a ternary.
Statement expressions are not standard C++ so I'd advise against using them.
A lambda
int b = []{
static_assert(2 > 1, "all is lost"); return 304;
}();
or
int b = ([]{static_assert(2 > 1, "all is lost");}, 304);
is hardly clean. (The second lambda looks like a hair's-breadth away from being undefined).