getml / reflect-cpp

A C++20 library for fast serialization, deserialization and validation using reflection. Supports JSON, BSON, CBOR, flexbuffers, msgpack, TOML, UBJSON, XML, YAML / msgpack.org[C++20]
https://rfl.getml.com
MIT License
1.13k stars 103 forks source link

Compile error with structures containing bitfields #159

Open loboernesto86 opened 4 months ago

loboernesto86 commented 4 months ago

Expected Behavior

No compilation error

Current Behavior

Structures containing bitfields give the compile errors below, packing seems to make no difference (was not expecting it to make any difference anyway)

...include\rfl\internal\bind_to_tuple.hpp(73,1): error C2664: 'std::tuple<uint8_t &,uint8_t &,uint8_t &,uint8_t &,uint64_t &> std::tie<uint8_t,uint8_t,uint8_t,uint8_t,uint64_t>(uint8_t &,uint8_t &,uint8_t &,uint8_t &,uint64_t &) noexcept': cannot convert argument 1 from 'uint8_t' to 'uint8_t &'
...include\rfl\internal\no_duplicate_field_names.hpp(24,19): error C2338: static_assert failed: 'Duplicate field names are not allowed in either named tuples or Literals.'

How to Reproduce

main.cpp


#include <iostream>
#include "rfl.hpp"
#include "Rfl/json.hpp"

#pragma pack(push,1)
struct DummyBitfieldPacked {
    uint8_t power_on : 1;    
    uint8_t error : 1;       
    uint8_t data_ready : 1;  
    uint8_t reserved : 5;    
    uint64_t data;           
};
#pragma pack(pop)

struct DummyBitfieldUnpacked {
    uint8_t power_on : 1;    
    uint8_t error : 1;       
    uint8_t data_ready : 1;  
    uint8_t reserved : 5;    
    uint64_t data;           
};

struct Dummy {
    bool power_on;   
    bool error;      
    bool data_ready; 
    uint8_t reserved;
    uint64_t data;           
};

int main(){    

    std::string json_string;

    //Errors  C2664 & C2338 
    // const DummyBitfieldPacked dummy_bitfields_packed = { 1, 0, 1, 0, 0x1234567890ABCDEF };
    //json_string = rfl::json::write(dummy_bitfields_packed);
    //std::cout << json_string << std::endl;

    //Errors C2664 & C2338 
    //const DummyBitfieldUnpacked dummy_bitfields_unpacked = { 1, 0, 1, 0, 0x1234567890ABCDEF };
    //json_string = rfl::json::write(dummy_bitfields_unpacked);
    //std::cout << json_string << std::endl;

    const Dummy dummy = {1, 0, 1, 0, 0x1234567890ABCDEF};
    json_string = rfl::json::write(dummy);
    std::cout << json_string << std::endl;

    std::cout << "done";
}

Context (Environment)

Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.10.1 /std:c++20 Windows 11

liuzicheng1987 commented 4 months ago

Thanks for the detailed issue. I will take a look!