Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/boost/pfr/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ constexpr auto structure_tie(T&&, std::enable_if_t< std::is_rvalue_reference<T&&
/// \endcode
template <class T, class F>
constexpr void for_each_field(T&& value, F&& func) {
return ::boost::pfr::detail::for_each_field(std::forward<T>(value), std::forward<F>(func));
::boost::pfr::detail::for_each_field(std::forward<T>(value), std::forward<F>(func));
}

/// \brief std::tie-like function that allows assigning to tied values from aggregates.
Expand Down
6 changes: 3 additions & 3 deletions include/boost/pfr/detail/core_name20_static.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,14 @@ template <class T, class F>
constexpr void for_each_field_with_name(T&& value, F&& func) {
return boost::pfr::detail::for_each_field(
std::forward<T>(value),
[f = std::forward<F>(func)](auto&& field, auto index) mutable {
[&func](auto&& field, auto index) mutable {
using IndexType = decltype(index);
using FieldType = decltype(field);
constexpr auto name = boost::pfr::detail::get_name<std::remove_reference_t<T>, IndexType::value>();
if constexpr (std::is_invocable_v<F, std::string_view, FieldType, IndexType>) {
f(name, std::forward<FieldType>(field), index);
std::forward<F>(func)(name, std::forward<FieldType>(field), index);
} else {
f(name, std::forward<FieldType>(field));
std::forward<F>(func)(name, std::forward<FieldType>(field));
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions include/boost/pfr/detail/for_each_field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ constexpr void for_each_field(T&& value, F&& func) {

::boost::pfr::detail::for_each_field_dispatcher(
value,
[f = std::forward<F>(func)](auto&& t) mutable {
[&func](auto&& t) mutable {
// MSVC related workaround. Its lambdas do not capture constexprs.
constexpr std::size_t fields_count_val_in_lambda
= boost::pfr::detail::fields_count<std::remove_reference_t<T>>();

::boost::pfr::detail::for_each_field_impl(
t,
std::forward<F>(f),
std::forward<F>(func),
detail::make_index_sequence<fields_count_val_in_lambda>{},
std::is_rvalue_reference<T&&>{}
);
Expand Down
11 changes: 11 additions & 0 deletions test/core/run/for_each_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ struct simple {

struct empty{};

struct stateful_counting_visitor {
std::size_t count = 0;

template <class T>
void operator()(const T&) { ++count; }
};

#if BOOST_PFR_USE_CPP17
constexpr std::size_t get_field_count_through_for_each_field() {
std::size_t counter = 0;
Expand Down Expand Up @@ -132,6 +139,10 @@ int main () {
});
BOOST_TEST_EQ("", ss.str());
ss.str("");

stateful_counting_visitor counting_visitor;
boost::pfr::for_each_field(simple{}, counting_visitor);
BOOST_TEST_EQ(3, counting_visitor.count);

return boost::report_errors();
}
10 changes: 10 additions & 0 deletions test/core_name/run/for_each_field_with_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ struct SimpleStruct {
std::string str;
};

struct stateful_counting_visitor {
std::size_t count = 0;

template <class T>
void operator()(std::string_view /*name*/, const T&) { ++count; }
};

int main () {
std::map<std::string, std::string> m;
Expand All @@ -27,5 +33,9 @@ int main () {
BOOST_TEST_EQ(m["c"], "e");
BOOST_TEST_EQ(m["str"], "test");

stateful_counting_visitor counting_visitor;
boost::pfr::for_each_field_with_name(SimpleStruct{}, counting_visitor);
BOOST_TEST_EQ(2, counting_visitor.count);

return boost::report_errors();
}