我想使用负数组索引来访问结构中紧接在该数组之前的相同类型成员。
考虑这种类型的代码:
union hello {
int a[2];
struct { int b0; int b1[1]; };
};
我想用来b1[-1]
访问b0
.
当我尝试这样做时,clang 和 gcc 似乎完全理解我想要什么。
extern const int test = hello{{42, 1337}}.b1[-1];
这在编译时正确地确定test
了42
.
-1
不幸的是,clang 会产生一个不受约束的警告。const
如果我更改为.gcc 也会这样做constexpr
。
编写此类代码的正确方法是什么?
以下是我已经知道但不喜欢的方式:
a[]
与基于 1 的索引一起使用。- 制作
b1
一个指向 的指针a[1]
。