reflect icon indicating copy to clipboard operation
reflect copied to clipboard

Arrays, Unions and Bitfields not working

Open momo5502 opened this issue 1 year ago • 3 comments

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

momo5502 avatar Aug 20 '24 09:08 momo5502

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.

kris-jusiak avatar Aug 20 '24 09:08 kris-jusiak

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 :(

momo5502 avatar Aug 20 '24 09:08 momo5502

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!

suy avatar Aug 20 '24 10:08 suy