1

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

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

Is there a way to make it work?

4
  • 9
    Array subscripting involves pointer arithmetic and dereferencing and cannot be used as such. What are you trying to accomplish by doing that? Commented Sep 10, 2023 at 0:53
  • 1
    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. Commented Sep 10, 2023 at 8:42
  • @tstanisl when that becomes available, how would I write it? like this: _Static_assert((constexpr int[]){2, 1, 0}[2], "err"); ? Commented Sep 10, 2023 at 8:57
  • Maybe, currently clang seams to support it but it does not work yet :/ Commented Sep 10, 2023 at 10:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.