Alternative metadata syntax/semantics #439
-
I would like to write the metadata directly on the field, instead of a separate struct like this: struct Person
{
Field<"name", std::string> name;
Field<"age", int> age;
Field<"country", std::string> country;
}; I have made an example on how this syntax could be enabled: https://godbolt.org/z/acheb5Y1v |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 7 replies
-
This is a neat concept and I appreciate your effort to put together a working example. I'm a bit hesitant of the design though. For one, while we could make these strings entirely static constexpr, I'd be afraid the compiler wouldn't be as efficient with packing and optimizing the struct. It's also annoying to have to call I've been thinking about creating a simple tool to generate the glaze metadata structs for a given C++ struct or header file. This way users would easily be able to automate their glz::meta generation. Allowing users to not have to change their structures and still serialize/deserialize without any performance degradation is a huge benefit of the current design. The benefit of your design is that there is a clear connection between the metadata and the variable itself, but I'm not sure that the proximity is worth the added complexity to generally program with the structs. I feel that the API should just be an additional layer that you can remove or include without affecting any of the core programming logic. Anyway, that's my philosophy, but I would love to hear more thoughts about this approach, pros and cons, and I'm always open to ways to improve the library. I just think this is a problem that should be solved by reflection, so whether that is creating a tool to generate the metadata, using compile builtins like Sincerely, |
Beta Was this translation helpful? Give feedback.
-
Thanks for your response, I totally understand if you don't want to implement this in your library, it was just a suggestion. To address the points you raised:
I might try and add this feature to the library if you are open to it, although I have looked a bit at the metadata stuff and it looks quite complex since I am not that familiar with the latest c++ and constexpr(I am also quite busy with work and school, but I guess you are too). Anyways your library is awesome, and I appreciate the effort you have put into it. Best regards, |
Beta Was this translation helpful? Give feedback.
-
Mathias, Thanks for your additional thoughts. I'm also quite allergic to macros, and even when we get reflection users will still have to use metadata for non-default behaviors. So, I am open to this approach, but cautious. To get this to work well with the architecture, and thus optimal performance, we would need to use structured binding magic to access the struct parameters, decode into a tuple, and then build the All the metadata would be stored as static constexpr values, so the struct should be nearly as efficient when defined this way. The biggest challenge I currently see is how do we get a member variable pointer from a normal variable? We have the class type, but I don't know of a means in C++ of merging the class type with the variable type to produce a member variable pointer at compile time. The reason member variable pointers are critical is that it allows us to do much more at compile time than normal pointers, because normal pointers are only known at runtime, but member variable pointers are just offsets that are known at compile time. It also allows us to produce much more efficient code by storing limited variants of member variable pointers and building compile time maps to these. So, I wouldn't want to implement this approach without figuring out how to get member variable pointers out at compile time. If you can help figure this out, that would be great. But, I'm not sure it's even possible. Thanks for the discussion! |
Beta Was this translation helpful? Give feedback.
-
I see that it is impossible to have this type of syntax for this library, since you cannot get a member pointer without proper reflection. You could probably write directly to the structured binding reference, with a similar level of optimization, but that wouldn't fit into the glz::object design. So in order to support this syntax, it would require major modifications and add extra complexity to the library, for a very small benefit. |
Beta Was this translation helpful? Give feedback.
-
After considering this syntax more, I've come to the conclusion that it isn't a good design for a variety of reasons. These features are all supported by the current approach of glaze, using member variable pointers, but are not supported with the field approach without additional code from the user and/or additional runtime cost. The field approach has the following issues:
|
Beta Was this translation helpful? Give feedback.
@mathiasgredal
After considering this syntax more, I've come to the conclusion that it isn't a good design for a variety of reasons. These features are all supported by the current approach of glaze, using member variable pointers, but are not supported with the field approach without additional code from the user and/or additional runtime cost.
The field approach has the following issues: