我想知道 GCC 是否会使用我创建的结构过度对齐的知识,并在生成比较 asm 时将其简单地视为一个整数。
它不是。我想知道这是否是由于 ABI 问题,我错过了其他一些因素,还是真的缺少优化?
代码:
struct S{
short a,b;
bool operator == (const S&) const = default;
};
struct alignas(alignof(int)) S2 {
short a,b;
bool operator == (const S2&) const = default;
};
bool f(const S& s){
constinit static S c{11,22};
return c==s;
}
bool f2(const S2& s2){
constinit static S2 c2{11,22};
return c2==s2;
}
static_assert(alignof(S)==2);
static_assert(alignof(S2)==4);