当尝试使用static_assert作为参数来评估逗号运算符时,编译失败
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? }它看起来像static_assert()甚至没有解决编译后void 。 我没有在标准中找到任何关于此的内容。 有没有办法使用它与逗号运算符或使用它与其他表达式(无分号)?
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)?
最满意答案
不,那里没有。 语言语法需要在静态声明结束时使用分号。
N4140§7[dcl.dcl] / 1
static_assert-declaration :
static_assert static_assert ( constant-expression , string-literal ) ;
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 ) ;
更多推荐
发布评论