Skip to content
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

Arrays, Unions and Bitfields not working #61

Open
momo5502 opened this issue Aug 20, 2024 · 3 comments
Open

Arrays, Unions and Bitfields not working #61

momo5502 opened this issue Aug 20, 2024 · 3 comments

Comments

@momo5502
Copy link

Thank you for this great library. I just wanted to report that arrays, unions and bitfields are all not working and causing compilation issues.
I can imagine supporting any of these 3 might be hard, but maybe there is a way to fix these, hence the report.
The godbolt sample shows the behaviour:

https://godbolt.org/z/r9KG8fEqE

@krzysztof-jusiak
Copy link
Contributor

Thanks @momo5502, yeah there are limitations of the technique. There are probably some ways to workaround some of it but not sure about unions and bitfields as that's really special.

One way which is to use opaque types with the same underlying representation, something like:

struct foo { 
    int a;

    struct
    {
      bitfield<int, 1> bit1;
      bitfield<int, 31> bitRest;
    } test;

     std::variant<int, float> x;
     std::array<char, 10> arr;
};

The above can work correctly (though current version needs small tweaking for full proper support which I'll fix) - https://godbolt.org/z/dGsv5Yc5b.

@momo5502
Copy link
Author

While that might certainly work, it's not an option for me. I'm using reflection for types from Windows.h and Microsoft, sadly, seems to love bitfields and unions :(

@suy
Copy link
Contributor

suy commented Aug 20, 2024

I appreciate a lot that you posted the issue and that Kris posted the workaround. I need to support a few legacy file formats, but thankfully I can write my types, so a variant and the bitfield trick (nice! I wasn't expecting that would work) could do for me. Thank you both!

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

No branches or pull requests

3 participants