Skip to content

Add tutorials for type-erasure, reflection and vanishing-this - #96

Open
jbcoe wants to merge 27 commits into
mainfrom
tutorials
Open

Add tutorials for type-erasure, reflection and vanishing-this#96
jbcoe wants to merge 27 commits into
mainfrom
tutorials

Conversation

@jbcoe

@jbcoe jbcoe commented Jul 22, 2026

Copy link
Copy Markdown
Owner

I'm not entirely sure that these tutorials are necessary.

The act of writing them was useful and perhaps they will be useful to readers of the (increasingly novel and technical) code in #95.

@jbcoe
jbcoe force-pushed the tutorials branch 2 times, most recently from 4d2bd17 to 321b94c Compare July 22, 2026 19:53
@jbcoe
jbcoe requested a review from Twon July 22, 2026 22:18
@jbcoe
jbcoe marked this pull request as ready for review July 22, 2026 22:18
@Twon

Twon commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

I think these are great, first 2 down and working through the 3rd now, but these are definitely useful (I'm learning reflection from the 3rd).

Perhaps the files could be renamed with a number indicating the order of the tutorials?

  1. 1_type_erasure.cc
  2. 2_vanishing_this_pointer
  3. 3_reflection.cc

The pedant in me finds it odd to start in the middle, go down and then go up in the file view in VSCode.

Comment thread tutorials/3_reflection.cc
Comment thread tutorials/3_reflection.cc
@jbcoe
jbcoe requested review from RyanJK5 and Twon July 23, 2026 09:54
Comment thread tutorials/2_vanishing_this_pointer.cc
Comment thread tutorials/3_reflection.cc
@jbcoe
jbcoe requested a review from RyanJK5 July 24, 2026 18:30

@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.

I just gave the tutorials an in-depth look, and I think they are very good! All of the information is accurate. I left some comments, mostly regarding additional pieces of the reflection tutorial worth acknowledging. Thanks for putting these together!

Comment thread tutorials/2_vanishing_this_pointer.cc
Comment thread tutorials/2_vanishing_this_pointer.cc
Comment thread tutorials/2_vanishing_this_pointer.cc Outdated
Comment thread tutorials/2_vanishing_this_pointer.cc Outdated
Comment thread tutorials/3_reflection.cc
Comment thread tutorials/3_reflection.cc Outdated
Comment thread tutorials/3_reflection.cc
Comment thread tutorials/3_reflection.cc
Comment thread tutorials/3_reflection.cc Outdated
Comment thread tutorials/3_reflection.cc
@jbcoe

jbcoe commented Jul 25, 2026

Copy link
Copy Markdown
Owner Author

@RyanJK5 thanks for the detailed review comments. This is exactly what’s needed for foundational material. Thanks for taking the time.

@jbcoe
jbcoe requested a review from RyanJK5 July 26, 2026 16:52

@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.

Looks good to me now, thanks for making those changes!

Comment thread tutorials/3_reflection.cc Outdated
@jbcoe
jbcoe requested a review from Twon July 28, 2026 20:23
Comment thread tutorials/3_reflection.cc
}

TEST(ReflectionHelpers, ClassifiesConstCorrectly) {
constexpr auto read_candidates =

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.

Personal I'd perfer concept constrained auto here because being unfamilar with this function, and with no explanation of its use, I had to go to C++ ref and read the documentation to be confident I know what was happening. I think the hint is a range is enough knowledge to skip this step

Suggested change
constexpr auto read_candidates =
constexpr std::ranges::range auto read_candidates =

@jbcoe jbcoe Jul 29, 2026

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

If you need to look at another tutorial to understand this one, that's a BAD sign.

I'll amend the use of auto here and elsewhere.

Comment thread tutorials/3_reflection.cc
TEST(ReflectionHelpers, ClassifiesConstCorrectly) {
constexpr auto read_candidates =
std::define_static_array(members_named(^^Widget, "read"));
constexpr auto write_candidates =

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.

Again concept-constrained auto

Suggested change
constexpr auto write_candidates =
constexpr std::ranges::range auto write_candidates =

Comment thread tutorials/3_reflection.cc
for (std::meta::info parameter : parameters_of(member)) {
if (!first) result += ",";
first = false;
result += display_string_of(dealias(type_of(parameter)));

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.

Ashame we don't have dynamic delimiters for range formatting yet. I feel like this is the line the magic happens on, but I have to wade through additional cognitive noise to get the magic... particularly in light of the example going on to then only use a single parameter per function. Perhaps add a multi-parameter overload or just simplify for one param

Comment thread tutorials/3_reflection.cc
}

TEST(ReflectionHelpers, SignatureDistinguishesOverloads) {
constexpr auto write_candidates =

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'm not so worried about adding concept-constrained auto once the user is exposed to how this method works from earlier examples

Comment thread tutorials/3_reflection.cc
// Adapted from
// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p2996r13.html
//
// struct data_member_options {

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 had to go to C++ ref to understand the significance of this (in part because there is no indication this is a std::meta type). I initially thought this was an example of a struct you were going to generate... Particularly as in count_ratio_specs it's constructed with {}.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

How can I make this clearly useful?

@RyanJK5, I added this comment in response to your suggestion but I think the reader is still missing some context. What should I add?

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.

How can I make this clearly useful?

@RyanJK5, I added this comment in response to your suggestion but I think the reader is still missing some context. What should I add?

I think the best way would to add a comment before the struct definition explaining what it is. Combining that with the explicit data_member_options in the lines below that @Twon mentioned in the other thread, it should convey the significance.

Comment thread tutorials/3_reflection.cc
consteval std::vector<std::meta::info> count_ratio_specs() {
std::vector<std::meta::info> specs;
// clang-format off
specs.push_back(data_member_spec(^^int, {.name = "count"}));

@Twon Twon Jul 29, 2026

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, for me, would connect the significance of the std::meta::data_member_options definition above and tell me it's a std::meta type.

Suggested change
specs.push_back(data_member_spec(^^int, {.name = "count"}));
specs.push_back(data_member_spec(^^int, data_member_options{.name = "count"}));

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.

3 participants