【发布时间】:2021-01-20 17:41:52
【问题描述】:
我正在尝试使用 clang-tidy(在使用 gcc 时)进行所有检查。我很好奇以下情况是否是 clang-tidy 的错误,因为它与我的编译器的警告相矛盾。这是一个最小的例子:
struct a{
virtual void func()=0;
virtual void func2()=0;
};
struct b:a{
void func() override {};
void func2() override {};
private:
int x{};
}
int main(){
b b_item;
}
建议:
/home/main.cpp:7:8: note: use "__attribute__((aligned(0)))" to align struct 'a' to 0 bytes
struct a{
^
/home/main.cpp:13:8: warning: accessing fields in struct 'b' is inefficient due to padding; only needs 4 bytes but is using 16 bytes [altera-struct-pack-align]
struct b:a{
^
由于clang-tidy做了很多更改,所以我不会在建议之后演示代码,重要的是:
struct a{
virtual void func()=0;
virtual void func2()=0;
} __attribute__((aligned(0)));
调用修复后的警告:
warning: requested alignment ‘0’ is not a positive power of 2 [-Wattributes]
12 | } __attribute__((aligned(0)));
【问题讨论】:
标签: c++ alignment memory-alignment clang-tidy