How can one modify a member of a boost hana struct

427 Views Asked by At

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.

1

There are 1 best solutions below

2
On

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);
});