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
900 stars 76 forks source link

rfl::Field not default constructible #4

Closed riidefi closed 10 months ago

riidefi commented 10 months ago

Is this boilerplate actually necessary?

rfl::Field<"children", std::vector<s32>> mChildren = std::vector<s32>();
liuzicheng1987 commented 10 months ago

You can use rfl::default_value as shown here:

https://github.com/getml/reflect-cpp/blob/main/docs/structs.md

And this is actually very much on purpose. It prevents you from accidentally forgetting fields, which can lead to very tricky runtime errors. The reasoning is explained here:

https://github.com/getml/reflect-cpp/blob/main/docs/custom_classes.md

It is technically not necessary at all. As a compromise, what we could do is to offer some kind of alternative syntax, maybe like this:

rfl::Field<"children", std::vector<s32>, rfl::default_constructible> mChildren

If you can come up with a more concise syntax, I'm open to suggestions.