-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Iterate over aggregate fields with the name #171
Iterate over aggregate fields with the name #171
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
Please see the comments to make the header changes smaller. Also some docs updates are required
include/boost/pfr/core_name.hpp
Outdated
/// \endcode | ||
template <class T, class F> | ||
constexpr void for_each_field_with_name(T&& value, F&& func) { | ||
return detail::for_each_field_with_name(std::forward<T>(value), std::forward<F>(func)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be implemented in a simpler way:
return boost::pfr::for_each_field(
std::forward<T>(value),
[f = std::forward<F>(func)](auto&& t, auto index) mutable {
constexpr auto name = boost::pfr::get_name<decltype(index)::value>();
if constexpr (std::is_invokable_v<F, ?????? >) {
f(name, std::forward<decltype(t)>(t), index);
} else {
f(name, std::forward<decltype(t)>(t));
}
});
|
||
}}} // namespace boost::pfr::detail | ||
|
||
#endif // BOOST_PFR_DETAIL_FOR_EACH_FIELD_WITH_NAME_IMPL_HPP |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update the documentation file https://github.com/boostorg/pfr/blob/develop/doc/pfr.qbk . Add after pfr_quick_examples_for_each another table row with pfr_quick_examples_for_each_with_name and add pfr_quick_examples_for_each_with_name example to https://github.com/boostorg/pfr/tree/develop/example
3d2748d
to
acab7fa
Compare
I updated my PR according to your comments. But on the github action I see a failure that I don't understand https://github.com/Baduit/pfr/actions/runs/9660279058/job/26645491447 |
To be able to iterate over the fields name and value of an aggregate
acab7fa
to
c203197
Compare
I updated my branch and thanks to your commit to fix the CI the builds are green |
Pull Request Test Coverage Report for Build 10586042298Details
💛 - Coveralls |
Many thanks for the PR! |
Add a new function
for_each_field_with_name
The goal is to be able to do this:
I think this is more user friendly than we way it can be done without this PR:
I tried to make the code of
for_each_field_with_name
as close as possible tofor_each_field
.I will add new tests if you think this feature could be merged in the future.
I have explored a bit the idea of completing
for_each_field
instead of creating a new function in this branch https://github.com/Baduit/pfr/tree/feature/for_each_field_can_provide_name, most of the work is done and it makes possible to do stuff like this:Do you think it is a better idea ?