Skip to content

ByteString: Adjust bitfields for MSVC #1446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jchadwick-buf
Copy link
Contributor

@jchadwick-buf jchadwick-buf commented Apr 17, 2025

Microsoft Visual C++ compiler has more restrictions than usual about bit fields. There are two restrictions that particularly break ByteString:

  • A consecutive field with a different type will force alignment to next boundary
  • The alignment boundary is based on the type of the bitfield

Because of this, we can't use the enumeration type inside of the bitfield. Actually though, this is mostly OK: the header struct is the only one that actually needs a typed enum, and nothing follows it in the header struct.

Therefore, this workaround should make all of GCC, Clang and MSVC happy.

Also: fixes the #pragma pack directives, which are typo'd slightly.

P.S.: Yes, it must be std::uint8_t and not uint8_t. It turns out MSVC actually ensures the typedefs are namespaced-only, if you include <cstdint>. This is correct AFAIK, although I've never seen behavior like that out of any of the other implementations. Dunno why this doesn't apply to size_t, too.

Related issue: #768

Microsoft Visual C++ compiler has more restrictions than usual about bit fields. There are two restrictions that particularly break ByteString:

- A consecutive field with a different type will force alignment to next boundary
- The alignment boundary is based on the type of the bitfield

Because of this, we can't use the enumeration type inside of the bitfield. Actually though, this is mostly OK: the header struct is the only one that actually needs a typed enum, and nothing follows it in the header struct.

Therefore, this workaround should make all of GCC, Clang and MSVC happy.

Also: fixes the #pragma pack directives, which are typo'd slightly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant