diff --git a/docs/named_tuple.md b/docs/named_tuple.md index ae0de8d06..7a90f2f17 100644 --- a/docs/named_tuple.md +++ b/docs/named_tuple.md @@ -103,15 +103,9 @@ auto person = rfl::Field<"first_name", std::string>("Bart") * rfl::Field<"last_name", std::string>("Simpson"); person.apply([](const auto& f) { - auto field_name = f.name(); - const auto& value = *f.value(); -}); - -person.apply([](Field& f) { // The field name can also be obtained as a compile-time constant. - constexpr auto field_name = Field::name(); - using field_pointer_type = typename Field::Type; - field_pointer_type* value = f.value(); + auto field_name = f.name(); + const auto& value = f.value(); }); ``` diff --git a/include/rfl/NamedTuple.hpp b/include/rfl/NamedTuple.hpp index db0f6dc63..3bb270e4f 100644 --- a/include/rfl/NamedTuple.hpp +++ b/include/rfl/NamedTuple.hpp @@ -185,17 +185,7 @@ class NamedTuple { /// Invokes a callable object once for each field in order. template - void apply(F&& _f) { - const auto apply_to_field = - [&_f](AFields&&... fields) { - ((_f(std::forward(fields))), ...); - }; - rfl::apply(apply_to_field, fields()); - } - - /// Invokes a callable object once for each field in order. - template - void apply(F&& _f) const { + void apply(F&& _f) const& { const auto apply_to_field = [&_f](const auto&... fields) { ((_f(fields)), ...); }; diff --git a/tests/json/test_apply_on_named_tuple.cpp b/tests/json/test_apply_on_named_tuple.cpp new file mode 100644 index 000000000..686e879f1 --- /dev/null +++ b/tests/json/test_apply_on_named_tuple.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_apply_on_named_tuple { + +TEST(json, test_apply_on_named_tuple) { + auto person = rfl::Field<"first_name", std::string>("Bart") * + rfl::Field<"last_name", std::string>("Simpson"); + + const auto print = [](const auto& f) { + const auto& name = f.name(); + const auto& value = f.value(); + std::cout << name << ", " << value << std::endl; + }; + + person.apply(print); + + EXPECT_EQ(rfl::json::write(person), + R"({"first_name":"Bart","last_name":"Simpson"})"); +} +} // namespace test_apply_on_named_tuple