ByteMe95
ByteMe95

Reputation: 876

How can one modify a member of a boost hana struct

I can see how one can easily read from an introspective boost hana struct generically by field/value, but I cant find any way to write into a struct generically.

Basically I would want to be able to do something like:

boost::hana::for_each( data, boost::hana::fuse( [](auto name, auto member){
     member = my_val_getter( name );
} ) );

but I cant seem to find a way to get a reference to "member" in order to be able to set it, if I try changing the method signature to auto & member it leads to all sorts of compilation errors.

Upvotes: 2

Views: 493

Answers (1)

ByteMe95
ByteMe95

Reputation: 876

looks like boost::hana::at_key is the trick, example usage can be found here

Relevant Code snippet:

hana::for_each(hana::keys(result), [&](auto key) {
    auto& member = hana::at_key(result, key);
    using Member = std::remove_reference_t<decltype(member)>;
    member = from_json<Member>(in);
});

Upvotes: 3

Related Questions