Skip to content

Add vanishing-this-pointer tutorial - #141

Open
jbcoe wants to merge 1 commit into
mainfrom
vanishing-this-tutorial
Open

Add vanishing-this-pointer tutorial#141
jbcoe wants to merge 1 commit into
mainfrom
vanishing-this-tutorial

Conversation

@jbcoe

@jbcoe jbcoe commented Aug 14, 2026

Copy link
Copy Markdown
Owner

No description provided.

@jbcoe
jbcoe marked this pull request as ready for review August 14, 2026 11:00
@jbcoe
jbcoe force-pushed the vanishing-this-tutorial branch 2 times, most recently from f818c77 to 8284c09 Compare August 14, 2026 13:59
Base automatically changed from polymorphism-tutorial to main August 14, 2026 14:08
@jbcoe
jbcoe force-pushed the vanishing-this-tutorial branch from 8284c09 to 081fa05 Compare August 14, 2026 14:08
@jbcoe
jbcoe requested a review from RyanJK5 August 14, 2026 14:13

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

Comment on lines +43 to +44
void* x_ptr = &a.x;
auto* a_ptr = static_cast<A*>(x_ptr);

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.

Suggested change
void* x_ptr = &a.x;
auto* a_ptr = static_cast<A*>(x_ptr);
auto* a_ptr = reinterpret_cast<A*>(x_ptr)

I am not entirely sure if the cast-to-void* is legal by the standard. My understanding is that void* can only be used for strict aliasing. Regardless, I think it's more precise to use reinterpret_cast here.


class A {
public:
Callable fn;

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 we may as well introduce the [[no_unique_address]] here. The section is already pretty light, so we could take a slight bit of complexity from the following section.

Comment on lines +85 to +86
int offset = reinterpret_cast<A*>(this)->value_;
return x + offset;

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.

Suggested change
int offset = reinterpret_cast<A*>(this)->value_;
return x + offset;
int y = reinterpret_cast<A*>(this)->value_;
return x + y;

The variable name offset may be a little confusing, since we're also using the term offset to refer to member variable offsets above.

Comment on lines +145 to +146
// Multiply::operator() must be defined out of line: the reinterpret_cast and
// static_cast require `MultiplyBase` and `A` to be complete types.

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.

Suggested change
// Multiply::operator() must be defined out of line: the reinterpret_cast and
// static_cast require `MultiplyBase` and `A` to be complete types.

I think saying this once is fine. The above comment could be updated to say "Add:operator() and Multiply::operator() must be defined out of line..."

Comment on lines +140 to +141
const auto* base = reinterpret_cast<const AddBase*>(this);
const auto* owner = static_cast<const A*>(base);

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.

Depending how const correct we want the tutorial to be, I think we should either remove const here, since the member function itself is marked non-const, or just add const to all the member function definitions.

Comment on lines +152 to +153
const auto* base = reinterpret_cast<const MultiplyBase*>(this);
const auto* owner = static_cast<const A*>(base);

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.

See comment above about const-ness.

Comment on lines +162 to +164
A a(3);
EXPECT_EQ(a.add(5), 8);
EXPECT_EQ(a.multiply(5), 15);

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.

Perhaps a little conclusive comment: "add() and multiply() now behave just like member functions on A."

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