C++ Logo

std-discussion

Advanced search

Pointer-interconvertibility between a standard-layout non-empty class object and its empty base class subobject

From: Kazutoshi Satoda <k_satoda_at_[hidden]>
Date: Tue, 8 Dec 2020 10:39:20 +0900
Given the code:

  struct B {};
  struct D : B { int m; };
  D d;

Are the object d and its base class subobject (of B) pointer-interconvertible?

I thought yes, and cppreference.com has an example implying yes.
https://en.cppreference.com/w/cpp/types/is_pointer_interconvertible_base_of
But now I can't be sure after re-reading the standard:
https://timsong-cpp.github.io/cppwp/n4861/basic.compound#4
> Two objects a and b are pointer-interconvertible if:
> — they are the same object, or
> — one is a union object and the other is a non-static data member of
> that object (11.5), or
> — one is a standard-layout class object and the other is
> the first non-static data member of that object, or, if the object
> has no non-static data members, any base class subobject of that
> object (11.4), or
> — there exists an object c such that a and c are
> pointer-interconvertible, and c and b are pointer-interconvertible.

The object d is a standard-layout class object, then the third condition
is relevant. The condition consists of two conditions:
  (1) the other is the first non-static data member of that object, or,
  (2) if the object has no non-static data members, any base class
      subobject of that object
The condition (1) is obviously not applicable. The condition (2) is also
not applicable because the object d has a non-static data member m.

Then the conclusion seems that the object d and its base class subobject
are not pointer-interconvertible, which means I and cppreference.com are
both wrong.

Is the conclusion correct? Am I missing something?


I found the wording was touched by CWG 2254.
https://wg21.cmeerw.net/cwg/issue2254
I think the replacement of "the first base class subobject" to
"any base class subobject" was OK since Empty-base-optimization is
mandated for standard-layout classes. But now I wonder that "if the
object has no non-static data members" should have been struck then.

-- 
k_satoda

Received on 2020-12-07 19:39:26