【发布时间】:2016-02-12 15:56:01
【问题描述】:
在以下结构中:
struct alignas(?) test
{
int32_t f1; // 4 bytes
int8_t f2; // 1 byte
int8_t f3; // 1 byte
};
如何使用alignas 使sizeof(test) 正好是6 个字节?
alignas(1) 不被编译器接受(gcc、msvc、clang)(错误如:error: requested alignment is less than minimum alignment of 4 for type 'test')。
UPD。当然,这个变体可以正常工作:
#pragma pack(push, 1)
struct alignas(?) test
{
int32_t f1; // 4 bytes
int8_t f2; // 1 byte
int8_t f3; // 1 byte
};
#pragma pack(pop)
但是有没有办法在没有预处理器的情况下只使用标准 C++11/14 来做到这一点?
【问题讨论】:
-
需要哪些成员?目前,您的三个成员将始终加起来最多 6 个字节
-
@NickStrupat 目前
sizeof(test)==8
标签: c++ memory-alignment alignas