Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[expr.static.cast] static_cast of pointer to derived class only handles base class subobjects #4605

Merged
merged 1 commit into from May 21, 2021

Conversation

randomnetcat
Copy link
Contributor

static_cast from base* to derived* only handles the case where the base is a base class subobject of a derived, not when base is a member subobject. This brings the pointer wording in line with the reference wording:

If the object of type “cv1 B” is actually a base class subobject of an object of type D, the result refers to the enclosing object of type D.

@jensmaurer
Copy link
Member

@tkoeppe, this looks sufficiently obvious to me to make it editorial. What do you think?

@tkoeppe
Copy link
Contributor

tkoeppe commented May 20, 2021

I agree. I'm not sure about the history, but if I had to speculate I'd guess that that wording is old, and leans on the "subtype" analogy, and may predate the current (entirely unrelated) notion of "subobject". (Maybe someone with an ARM could check?) The change seems an outright improvement. Feel free to merge!

@jensmaurer
Copy link
Member

The ARM didn't have static_cast, but the text about explicit type conversions on page 68 reads:

"Such a cast from a base to a derived class assumes that the object of the base class is a sub-object of an object of the derived class; the resulting pointer points to the enclosing object of the derived class."

Note that this puts "enclosing" in sufficient context to exclude the member case.

@jensmaurer jensmaurer merged commit 046c4d3 into cplusplus:master May 21, 2021
@randomnetcat randomnetcat deleted the static-cast-base branch May 21, 2021 18:24
@tkoeppe
Copy link
Contributor

tkoeppe commented May 21, 2021

Great, thanks for adding this historical context!

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.

None yet

4 participants