“结构因对齐说明符而被填充”VS 警告

‘structure was padded due to alignment specifier’ VS warning

如果在 Visual Studio 中我为 class 或结构指定对齐方式,例如

struct __declspec(align(256)) A
{
};

我收到 4 级警告,如下所示“警告 C4324:'A':结构因对齐说明符而被填充”。 我是否以某种方式错误地指定了对齐方式,或者可以安全地忽略此警告?

Do I specify alignment somehow incorrectly

不,尽管您使用的是语言扩展。这可能是不必要的,因为有一个更好的标准语法:

struct alignas(256) A
{
};

This warning is just safe to ignore?

是的,忽略此警告是安全的,除非您有理由认为填充有问题。

但是,我建议问问自己:为什么 class 没有成员时需要对齐?