带有static

编程入门 行业动态 更新时间:2024-10-18 20:18:49
带有static_assert()的逗号运算符(Comma operator with static_assert())

当尝试使用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-expressionstring-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 ) ;

更多推荐

本文发布于:2023-08-02 19:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379617.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:static

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!