Search code examples
c++boostboost-hana

is there any way to index the hana adapted struct by its member name?


I have a hana defined struct, I knew I can iterate through it, but I wonder how can I index it by member name?

#include <string>
#include <boost/hana.hpp>
int main()
{
    struct Person {
        BOOST_HANA_DEFINE_STRUCT(Person,
            (std::string, name),
            (int, age)
        );
    };
    Person john{"john Dow", 30};

    return 0;
}

Is there any hana tools allow me to do get(john, "name") and return john Dow?


Solution

  • Turns out there is at_key. we can use hana::at_key(obj, key)