c++runtimestatic-assert

Comma operator with static_assert()


When trying to evaluate comma operator with static_assert as an argument compilation fails

void fvoid() {}

int main() {
    int a = (1, 2); // a=2
    int b = (fvoid(), 3); // b=3

    int d = ( , 5);
    //        ^
    // error: expected primary-expression before ',' token. OK

    int c = (static_assert(true), 4);
    //       ^~~~~~~~~~~~~
    // error: expected primary-expression before 'static_assert'. Why?
}

It looks like that static_assert() doesn't even resolve to void after compiling. I didn't manage to find anything regarding this in standard. Is there a way to use it with comma operator or use it in line with other expression (without semicolon)?


Solution

  • No, there is not. The language grammar requires a semicolon at the end of the static assert declaration.

    N4140 §7 [dcl.dcl]/1

    static_assert-declaration:

    ​ ​ ​ ​static_assert ( constant-expression , string-literal ) ;