I'd like to use a negative array index to access the same-type member that immediately precedes that array in a struct.
Consider this type of code:
union hello {
int a[2];
struct { int b0; int b1[1]; };
};
I want to use b1[-1] to access b0.
When I try to do this, clang and gcc seem to understand exactly what I want.
extern const int test = hello{{42, 1337}}.b1[-1];
This correctly determines at compile time that test is really 42.
Unfortunately, clang produces a warning that -1 is not in bound. Gcc does too if I change const to constexpr.
What is the correct way to write this type of code?
Here are the ways I already know but don't like:
- Use
a[]with 1-based indexing. - Make
b1a pointer that points toa[1].
If I understand your question correctly, you have variables {c0, c1, c2, c3,...}, and sometimes you want to treat them as an array [c1, c2, c3,...], an at other times as an array [c0, c1, c2, c3,...].
(I'm not sure I understand why you want to do this, but never mind.)
Here's one solution:
Now you can iterate over A[i] if you want to include c0, and over B[i] if you don't.