Skip to content

Reflection tutorial - #146

Open
jbcoe wants to merge 5 commits into
cpp26-infrafrom
reflection-tutorial
Open

Reflection tutorial#146
jbcoe wants to merge 5 commits into
cpp26-infrafrom
reflection-tutorial

Conversation

@jbcoe

@jbcoe jbcoe commented Aug 14, 2026

Copy link
Copy Markdown
Owner

No description provided.

@jbcoe
jbcoe changed the base branch from main to vanishing-this-tutorial August 14, 2026 17:49
@jbcoe
jbcoe force-pushed the reflection-tutorial branch 2 times, most recently from 78421b5 to 449e861 Compare August 17, 2026 22:47
@jbcoe
jbcoe force-pushed the reflection-tutorial branch from 449e861 to d88c703 Compare August 17, 2026 23:44
Base automatically changed from vanishing-this-tutorial to main August 18, 2026 07:47
@jbcoe
jbcoe force-pushed the reflection-tutorial branch 2 times, most recently from dfc0352 to 1ee1e03 Compare August 18, 2026 07:48
@jbcoe
jbcoe changed the base branch from main to cpp26-infra August 18, 2026 08:27
@jbcoe
jbcoe force-pushed the reflection-tutorial branch from 1ee1e03 to 7b843bd Compare August 18, 2026 08:28
@jbcoe
jbcoe marked this pull request as ready for review August 18, 2026 08:37
@jbcoe
jbcoe force-pushed the reflection-tutorial branch from 4827585 to 4ac10e1 Compare August 18, 2026 12:08
@jbcoe
jbcoe requested a review from RyanJK5 August 18, 2026 16:57
@jbcoe
jbcoe force-pushed the reflection-tutorial branch from 4ac10e1 to 3a2459f Compare August 18, 2026 21:18
@jbcoe
jbcoe force-pushed the reflection-tutorial branch 4 times, most recently from 2d2ddfe to ad90164 Compare August 18, 2026 21:37
@jbcoe
jbcoe force-pushed the reflection-tutorial branch from ad90164 to 91ce8a7 Compare August 18, 2026 22:36

@RyanJK5 RyanJK5 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really happy with how this came out. It keeps the relevant parts of reflection in clear focus.

Comment thread tutorials/reflection.cc
Comment on lines +82 to +85
// We use `throw` inside a `consteval` block rather than `static_assert`
// or `EXPECT_*`: `static_assert` needs `members` to independently be a
// constant expression, and `EXPECT_*`'s comparison helpers aren't
// `constexpr` functions.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code probably do with a one-sentence explanation of what a consteval block is.

Comment thread tutorials/reflection.cc
// `e` is a loop variable, not a constant expression, so we can't
// splice it with `return [:e:];`. `extract<Suit>(e)` only needs a
// value, not a fixed compile-time constant.
return extract<Suit>(e);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like how naturally this was introduced.

Comment thread tutorials/reflection.cc
Comment on lines +280 to +290
TEST(TutorialsReflection, Substitute) {
// `substitute(^^Template, {args...})` instantiates a template from infos;
// `reflect_constant` turns an ordinary value into an info usable as a
// non-type template argument.
// clang-format off
constexpr std::meta::info array_info =
substitute(^^std::array, {^^double, std::meta::reflect_constant(3)});
// clang-format on

static_assert(std::is_same_v<typename[:array_info:], std::array<double, 3>>);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could benefit from a comment explaining the use case of substitute - when you want to use a reflection in a consteval context even though it itself is not a constant expression.

For me, at least, I found the distinction between constant expressions and consteval to be one of the more difficult topics when first learning reflection.

Comment thread tutorials/reflection.cc
Comment on lines +123 to +128
constexpr std::ranges::range auto member_functions = std::define_static_array(
members_of(^^Point, std::meta::access_context::current()) |
std::views::filter([](std::meta::info member) {
return is_function(member) && has_identifier(member);
}));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would prefer to write this as two separate filters, but I'm more than happy to approve whatever you or others find more readable. (below code is not clang-formatted)

  constexpr std::ranges::range auto member_functions = std::define_static_array(
      members_of(^^Point, std::meta::access_context::current())
      | std::views::filter(std::meta::is_function)
      | std::views::filter(std::meta::has_identifier));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants