Skip to content

Commit

Permalink
slight rework
Browse files Browse the repository at this point in the history
clang on linux doesn't believe field.name() can be constexpr, so
fall back to Field::name() when constexpr is required.
  • Loading branch information
grandseiken committed Feb 7, 2024
1 parent fb0dd37 commit 34c36d9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ const auto view = rfl::to_view(lisa);
view.apply([](const auto& f) {
// f is a an rfl::Field pointing to the original field.
// f.name() is a compile-time constant.
std::cout << f.name() << ": " << rfl::json::write(*f.value()) << std::endl;
});
```
Expand Down
4 changes: 3 additions & 1 deletion docs/named_tuple.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ auto person = rfl::Field<"first_name", std::string>("Bart") *
rfl::Field<"last_name", std::string>("Simpson");

person.apply([](const auto& f) {
constexpr auto field_name = f.name();
auto field_name = f.name();
const auto& value = *f.value();
});

person.apply([]<typename Field>(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();
});
Expand Down
6 changes: 3 additions & 3 deletions tests/json/test_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ void test() {

view.apply([]<typename Field>(const Field& field) {
using field_type = std::remove_pointer_t<typename Field::Type>;
if constexpr (field.name() == "age") {
if constexpr (Field::name() == "age") {
static_assert(std::is_same_v<field_type, int>);
} else {
static_assert(std::is_same_v<field_type, std::string>);
}
});

rfl::to_view(lisa).apply([](auto&& field) {
if constexpr (field.name() == "first_name") {
rfl::to_view(lisa).apply([](auto field) {
if constexpr (decltype(field)::name() == "first_name") {
*field.value() = "Bart";
}
});
Expand Down

0 comments on commit 34c36d9

Please sign in to comment.