gpt4 book ai didi

How to use an array-literal with _Static_assert in C?(如何在C中使用数组文字WITH_STATIC_ASSERT?)

转载 作者:bug小助手 更新时间:2023-10-26 20:51:49 27 4
gpt4 key购买 nike



I want to use an array-literal of literals in a _Static_assert, but I get a compiler error saying this is not a constant expression

我希望在a_static_assert中使用文字的数组文字,但我收到编译器错误,指出这不是常量表达式


_Static_assert((int[]){2, 1, 0}[2], "err");

Is there a way to make it work?

有没有办法让它发挥作用?


更多回答

Array subscripting involves pointer arithmetic and dereferencing and cannot be used as such. What are you trying to accomplish by doing that?

数组下标涉及指针运算和取消引用,因此不能这样使用。你这样做的目的是什么?

The value is array object is not a compilation time constant. So it cannot be used in _Static_assert. The C23 will introduce constexpr and allow it for storage specifier of compound literal what may make your code work.

值为数组对象不是编译时间常量。因此它不能在_STATIC_ASSERT中使用。C23将引入常量,并允许它作为复合文字存储说明符,这可能会使您的代码工作。

@tstanisl when that becomes available, how would I write it? like this: _Static_assert((constexpr int[]){2, 1, 0}[2], "err"); ?

@tstanisl当它变得可用时,我将如何编写它?如:_STATIC_ASSERT((constexpr int[]){2,1,0}[2],“err”);?

Maybe, currently clang seams to support it but it does not work yet :/

也许,目前它似乎在支持它,但它还没有奏效:/

优秀答案推荐
更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com