mardi 18 août 2020

Modern way to access struct fields by name in C++

I wanted to check if there's an intuitive and easy way to access struct fields by name in modern C++. I am aware that similar questions have been asked and answered, and C++ reflection is a well investigated subject. I've came across libraries like boost-hana, boost-reflect, visit_struct, magic_get:

https://fossies.org/linux/boost/libs/hana/example/tutorial/introspection.json.cpp

https://github.com/garbageslam/visit_struct

https://github.com/apolukhin/magic_get

But the common point in all these approaches is that, they only allow you to get the total number of fields within the struct or do a certain operation in for_each manner for all the fields of the struct.

Yes, I can obviously check for the specific "name" of the field I'm looking for, by using the for_each functionality provided by these libraries. But I just wanted to check if there is any other trivial/well-know library that already does this. I would like to be able to deal with arbitrary number of nested structs, which is why I'm looking for something out of the box.

THanks





Aucun commentaire:

Enregistrer un commentaire