gpt4 book ai didi

c++ - Clang 和 MSVC 为同一结构报告不同的 `sizeof`

转载 作者:太空狗 更新时间:2023-10-29 21:35:28 26 4
gpt4 key购买 nike

clang :3.8.0MSVC:19.00.24215.1 用于 x64

是什么导致了编译器之间的这种奇怪差异? MSVC 报告 12,但 Clang 报告 8!这里的正确/预期行为是什么?标准对此有何规定?

enum class CodeCompletionDeclKind {};

struct SwiftSemanticToken {
unsigned ByteOffset;
unsigned Length : 24;
CodeCompletionDeclKind Kind : 6;
bool IsRef : 1;
bool IsSystem : 1;
};
static_assert(sizeof(SwiftSemanticToken) == 8, "Too big");

int main()
{
std::cout << "Hello, world!\n";
}

最佳答案

包含位字段的类对象的大小将由实现定义。

class.bit/1 :

...Allocation of bit-fields within a class object is implementation-defined. Alignment of bit-fields is implementation-defined. Bit-fields are packed into some addressable allocation unit. [ Note: Bit-fields straddle allocation units on some machines and not on others. Bit-fields are assigned right-to-left on some machines, left-to-right on others.  — end note ]

关于c++ - Clang 和 MSVC 为同一结构报告不同的 `sizeof`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42314104/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com