Mapping between members of the struct and it's json counterpart #1397
-
Hello, if I have any given struct, for example:
This is taken from the reflection example from godbolt. Is there any syntax for getting the mapped string for the json key?
The output here being: i |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Currently no, this mode of reflection is quite tricky to implement until C++26. You can call You could probably hack a solution that works for standard layout types that have known packed behavior (e.g. equal sized elements) by computing the distance between the pointer to the object instance and the pointer to the reference, but this would have to be very specialized. This would give you the index to use in the code above. It essentially comes down to reflection working through structured bindings, which are converted to tuples and thus are only accessed by index. There is the possibility of looking at pointer distances from the transformed tuple references at compile time. I'll have to think about this more. Thus far I have not seen this achieved in C++. |
Beta Was this translation helpful? Give feedback.
Currently no, this mode of reflection is quite tricky to implement until C++26. You can call
glz::reflect<my_struct>::keys[0]
which will return"i"
, but this requires you to know the index ofi
.You could probably hack a solution that works for standard layout types that have known packed behavior (e.g. equal sized elements) by computing the distance between the pointer to the object instance and the pointer to the reference, but this would have to be very specialized. This would give you the index to use in the code above.
It essentially comes down to reflection working through structured bindings, which are converted to tuples and thus are only accessed by index. There is the possibilit…