Skip to content

Commit 9e3f818

Browse files
Made sure that apply can never move or modify the tuple values; fixes #439 (#440)
1 parent eb8ae35 commit 9e3f818

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

docs/named_tuple.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,9 @@ auto person = rfl::Field<"first_name", std::string>("Bart") *
103103
rfl::Field<"last_name", std::string>("Simpson");
104104

105105
person.apply([](const auto& f) {
106-
auto field_name = f.name();
107-
const auto& value = *f.value();
108-
});
109-
110-
person.apply([]<typename Field>(Field& f) {
111106
// The field name can also be obtained as a compile-time constant.
112-
constexpr auto field_name = Field::name();
113-
using field_pointer_type = typename Field::Type;
114-
field_pointer_type* value = f.value();
107+
auto field_name = f.name();
108+
const auto& value = f.value();
115109
});
116110
```
117111

include/rfl/NamedTuple.hpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,7 @@ class NamedTuple {
185185

186186
/// Invokes a callable object once for each field in order.
187187
template <typename F>
188-
void apply(F&& _f) {
189-
const auto apply_to_field =
190-
[&_f]<typename... AFields>(AFields&&... fields) {
191-
((_f(std::forward<AFields>(fields))), ...);
192-
};
193-
rfl::apply(apply_to_field, fields());
194-
}
195-
196-
/// Invokes a callable object once for each field in order.
197-
template <typename F>
198-
void apply(F&& _f) const {
188+
void apply(F&& _f) const& {
199189
const auto apply_to_field = [&_f](const auto&... fields) {
200190
((_f(fields)), ...);
201191
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <cassert>
2+
#include <iostream>
3+
#include <rfl.hpp>
4+
#include <rfl/json.hpp>
5+
#include <string>
6+
#include <type_traits>
7+
#include <vector>
8+
9+
#include "write_and_read.hpp"
10+
11+
namespace test_apply_on_named_tuple {
12+
13+
TEST(json, test_apply_on_named_tuple) {
14+
auto person = rfl::Field<"first_name", std::string>("Bart") *
15+
rfl::Field<"last_name", std::string>("Simpson");
16+
17+
const auto print = [](const auto& f) {
18+
const auto& name = f.name();
19+
const auto& value = f.value();
20+
std::cout << name << ", " << value << std::endl;
21+
};
22+
23+
person.apply(print);
24+
25+
EXPECT_EQ(rfl::json::write(person),
26+
R"({"first_name":"Bart","last_name":"Simpson"})");
27+
}
28+
} // namespace test_apply_on_named_tuple

0 commit comments

Comments
 (0)