Skip to content

Add tutorial on narrowing conversions for wide pointers - #184

Draft
jbcoe wants to merge 3 commits into
mainfrom
jbcoe-narrowing-conversions
Draft

Add tutorial on narrowing conversions for wide pointers#184
jbcoe wants to merge 3 commits into
mainfrom
jbcoe-narrowing-conversions

Conversation

@jbcoe

@jbcoe jbcoe commented Aug 27, 2026

Copy link
Copy Markdown
Owner

No description provided.

// Handles an AnimalPtr by looking up (or, on first use, building) a
// cached BirdVtable for its vtable_, rather than owning one itself or
// wrapping the AnimalPtr.
BirdPtr(AnimalPtr* animal)

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.

This needs to be AnimalPtr - take pointers, even wide pointers, by value.

EXPECT_EQ(bird.noise(), "Screech");
});
}
// jthreads join automatically when destroyed, so no explicit join loop.

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.

Remove this redundant comment.

AnimalPtr eagle_view(&eagle);

// Two lookups against the same AnimalPtr share a cache entry; a
// different AnimalPtr gets its own.

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.

Remove this redundant comment.

EXPECT_EQ(duck_view.noise(), "Quack");
EXPECT_EQ(duck_view.identity(), "Duck");

// Constructed from a pointer to duck_view, not from `duck` itself.

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.

Remove this redundant comment.

std::string_view identity() const { return "Eagle"; }
};

// BirdVtable is narrower than AnimalVtable in the same way a Bird is a

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.

Rewrite this mostly redundant comment.

Comment on lines +29 to +30

// A follow-up to the tutorial on polymorphism. We construct a narrow

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.

Rewrite this comment to avoid clumsy overloading and re-use of wide and narrow.

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.

1 participant