Possible defect: Impossible conversion from B to D is mandated?

70 views
Skip to first unread message

Kazutoshi Satoda

unread,
Jan 4, 2017, 7:42:03 AM1/4/17
to std-dis...@isocpp.org
from N4618 5.2.9 [expr.static.cast] p2
> An lvalue of type "cv1 B", where B is a class type, can be cast to type
> "reference to cv2 D", where D is a class derived (Clause 10) from B, if
> cv2 is the same cv-qualification as, or greater cv-qualification than,
> cv1. If B is a virtual base class of D or a base class of a virtual base
> class of D, or if no valid standard conversion from "pointer to D" to
> "pointer to B" exists (4.11), the program is ill-formed. An xvalue of
> type "cv1 B" can be cast to type "rvalue reference to cv2 D" with the
> same constraints as for an lvalue of type "cv1 B". *If the object of
> type "cv1 B" is actually a subobject of an object of type D, the result
> refers to the enclosing object of type D.* Otherwise, the behavior is
> undefined.

Note that "If the object ... is actually a subobject ..." does not
qualify as "a base class subobject". Without qualifying as "base class
subobject", I think the above wording mandates impossible conversions
like this:

struct B {};
struct D : B { B b; };
D d;
B& rb = d.b; // Since the object referred by rb is actually a subobject of d,
D& rd = static_cast<D&>(rb); // rd must refer the enclosing d.

But of course, it doesn't work.
http://melpon.org/wandbox/permlink/VOVaW96txf57Zow6

I propose to replace "a subobject" to "a base class subobject".
https://github.com/k-satoda/cplusplus-draft/commit/753f87e351af03afb5389a5cbb4c024193d9040b

Is this need a core issue?

I think the intent is clear, so it is possible to see this as an
editorial fix (to fix it quickly via a pull request as is).

--
k_satoda

Nicol Bolas

unread,
Jan 4, 2017, 1:29:26 PM1/4/17
to ISO C++ Standard - Discussion
It should be noted that p11 of the same section has the same issue. It deals with pointers casts.

Richard Smith

unread,
Jan 4, 2017, 9:56:45 PM1/4/17
to std-dis...@isocpp.org
I would prefer this go by the core issue route. Yes, it's an obvious fix, and obviously matches the intent, but it's still a normative change. Can you forward this to Mike, along with Nicol's observation about p11? Since you can provide suggested wording, we should be able to move this into the standard in Kona.
 
I think the intent is clear, so it is possible to see this as an
editorial fix (to fix it quickly via a pull request as is).

--
k_satoda

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+unsubscribe@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.

Jens Maurer

unread,
Jan 5, 2017, 7:02:43 AM1/5/17
to std-dis...@isocpp.org

I sent this to Mike yesterday. I haven't heard back from him, though.

Jens


On 01/05/2017 03:56 AM, Richard Smith wrote:
> On 4 January 2017 at 04:41, Kazutoshi Satoda <k_sa...@f2.dion.ne.jp <mailto:k_sa...@f2.dion.ne.jp>> wrote:
>
> from N4618 5.2.9 [expr.static.cast] p2
> > An lvalue of type "cv1 B", where B is a class type, can be cast to type
> > "reference to cv2 D", where D is a class derived (Clause 10) from B, if
> > cv2 is the same cv-qualification as, or greater cv-qualification than,
> > cv1. If B is a virtual base class of D or a base class of a virtual base
> > class of D, or if no valid standard conversion from "pointer to D" to
> > "pointer to B" exists (4.11), the program is ill-formed. An xvalue of
> > type "cv1 B" can be cast to type "rvalue reference to cv2 D" with the
> > same constraints as for an lvalue of type "cv1 B". *If the object of
> > type "cv1 B" is actually a subobject of an object of type D, the result
> > refers to the enclosing object of type D.* Otherwise, the behavior is
> > undefined.
>
> Note that "If the object ... is actually a subobject ..." does not
> qualify as "a base class subobject". Without qualifying as "base class
> subobject", I think the above wording mandates impossible conversions
> like this:
>
> struct B {};
> struct D : B { B b; };
> D d;
> B& rb = d.b; // Since the object referred by rb is actually a subobject of d,
> D& rd = static_cast<D&>(rb); // rd must refer the enclosing d.
>
> But of course, it doesn't work.
> http://melpon.org/wandbox/permlink/VOVaW96txf57Zow6 <http://melpon.org/wandbox/permlink/VOVaW96txf57Zow6>
>
> I propose to replace "a subobject" to "a base class subobject".
> https://github.com/k-satoda/cplusplus-draft/commit/753f87e351af03afb5389a5cbb4c024193d9040b <https://github.com/k-satoda/cplusplus-draft/commit/753f87e351af03afb5389a5cbb4c024193d9040b>
>
> Is this need a core issue?
>
>
> I would prefer this go by the core issue route. Yes, it's an obvious fix, and obviously matches the intent, but it's still a normative change. Can you forward this to Mike, along with Nicol's observation about p11? Since you can provide suggested wording, we should be able to move this into the standard in Kona.
>
>
> I think the intent is clear, so it is possible to see this as an
> editorial fix (to fix it quickly via a pull request as is).
>
> --
> k_satoda
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to std-discussio...@isocpp.org <mailto:std-discussion%2Bunsu...@isocpp.org>.
> To post to this group, send email to std-dis...@isocpp.org <mailto:std-dis...@isocpp.org>.
> Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/ <https://groups.google.com/a/isocpp.org/group/std-discussion/>.
>
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to std-discussio...@isocpp.org <mailto:std-discussio...@isocpp.org>.
> To post to this group, send email to std-dis...@isocpp.org <mailto:std-dis...@isocpp.org>.

Kazutoshi Satoda

unread,
Jan 5, 2017, 1:41:22 PM1/5/17
to std-dis...@isocpp.org
On 2017/01/05 3:29 +0900, Nicol Bolas wrote:
> It should be noted that p11 of the same section has the same issue. It
> deals with pointers casts.

Yes, the proposed fix (on github) covers p11 as well. I forgot to
mention it. Thank you.

--
k_satoda

Kazutoshi Satoda

unread,
Jan 5, 2017, 1:41:34 PM1/5/17
to std-dis...@isocpp.org
On 2017/01/05 21:02 +0900, Jens Maurer wrote:
> I sent this to Mike yesterday. I haven't heard back from him, though.

Thank you. I'll sit and wait for the progress.

--
k_satoda
Reply all
Reply to author
Forward
0 new messages