[Bug c++/11867] [3.3/3.4 Regression] static_cast ignores ambiguity

mark at codesourcery dot com gcc-bugzilla@gcc.gnu.org
Sun Sep 7 19:17:00 GMT 2003


PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11867



------- Additional Comments From mark at codesourcery dot com  2003-09-07 19:17 -------
Subject: Re:  [3.3/3.4 Regression] static_cast ignores
	ambiguity

> struct A {};
> 
> struct B : private A {};
> 
> int
> main ()
> {
>   B b;
>   A* ap = (A*) &b;
>   B* bp = static_cast<B*> (ap); //Error
> }

This program is invalid.

The reason is that the only static_cast alternative that would allow
this conversion is the:

  An rvalue of type "pointer to cv1 B", where B is a class type, can  be
  converted  to an rvalue of type "pointer to cv2 D", where D is a class
  derived (clause _class.derived_) from B, if a valid  standard  conver-
  sion from "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is
  the same cv-qualification as, or greater cv-qualification  than,  cv1,
  and  B  is  not  a  virtual  base  class of D.

The "valid standard conversion" clause must be read to imply that this
restriction from [conv.ptr] applies to the conversion:

  If B is an inaccessible (clause _class.access_) or ambiguous
  (_class.member.lookup_) base class of  D, a program that necessitates
  this conversion is ill-formed.

Otherwise, there is nothing in the static_cast language to eliminate a
conversion from an ambiguous base to a derived class.

In fact, this was examined by the committee back in 1998, and is Core
Issue 54:

=====================

54. Static_cast from private base to derived class
Section: 5.2.9  expr.static.cast     Status: NAD     Submitter: Steve
Adamczyk     Date: 13 Oct 1998

Is it okay to use a static_cast to cast from a private base class to a
derived class? That depends on what the words "valid standard
conversion" in paragraph 8 mean — do they mean the conversion exists, or
that it would not get an error if it were done? I think the former was
intended — and therefore a static_cast from a private base to a derived
class would be allowed.

Rationale (04/99): A static_cast from a private base to a derived class
is not allowed outside a member from the derived class, because 4.10 
conv.ptr paragraph 3 implies that the conversion is not valid. (Classic
style casts work.)

=====================



More information about the Gcc-bugs mailing list