getml / reflect-cpp

A C++20 library for fast serialization, deserialization and validation using reflection. Supports JSON, BSON, CBOR, flexbuffers, msgpack, TOML, XML, YAML / msgpack.org[C++20]
https://getml.github.io/reflect-cpp/
MIT License
821 stars 65 forks source link

When declaring a char array in structure, a compile error occurs at a certain size. #64

Open JJeonHg opened 5 months ago

JJeonHg commented 5 months ago
#include <iostream>
#include <rfl.hpp>

struct Foo
{
    //std::string attr01;    // Compile OK
    //std::string attr02;    // Compile OK 
    //std::string attr03;    // Compile 0K

    //char attr01[500];      // Compile OK
    char attr01[600];        // Compile Error
    //char attr02[100];
    //char attr03[200];
    //char attr04[600];
    //char attr05[600];
    //char attr06[600];
    //char attr07[600];
    //char attr08[600];
    //char attr09[600];
    //char attr10[600];
    //char attr11[600];
    //char attr12[600];
    //char attr13[600];
    //char attr14[600];
    //char attr15[600];
    //char attr16[600];
    //char attr17[600];
    //char attr18[600];
    //char attr19[600];
    //char attr20[600];
};

int main() {

    for (const auto& f : rfl::fields<Foo>()) {
        std::cout << "name: " << f.name() << ", type: " << f.type() << std::endl;
    }
}
ChemiCalChems commented 5 months ago

Could you add what the compiler error is?

liuzicheng1987 commented 5 months ago

I'm guessing it's a compile-time recursion error...

Quick question: Why would you want to do this? Why not just use std::string?

JJeonHg commented 4 months ago

Previous system was char array. I want to know the field names and sizes of the structure

liuzicheng1987 commented 4 months ago

@JJeonHg , if you are deserializing this, it would expect an int array of exactly length 600. I don’t think this is what you were trying to accomplish.

The problem is that C Arrays are pretty annoying to handle, particularly in this context. I am not sure that there is a solution for this problem.

However, what I do know is that if you replace this by either std::string or std::array, it will work.

But I am going to think about it…maybe I can come up with a solution.