GCC Bugzilla has been upgraded from version 4.4.9 to 5.0rc3. If you see any problem, please report it to bug 64968.
Bug 11431 - static_cast behavior with subclasses when default constructor available
Summary: static_cast behavior with subclasses when default constructor available
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.3.1
: P1 critical
Target Milestone: 3.3.1
Assignee: Mark Mitchell
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-07-04 09:26 IST by nicolas.burrus@lrde.epita.fr
Modified: 2003-07-06 03:47 IST (History)
1 user (show)

See Also:
Host: i386-pc-linux-gnu
Target: i386-pc-linux-gnu
Build: i386-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed: 2003-07-04 21:47:50


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description nicolas.burrus@lrde.epita.fr 2003-07-04 09:26:34 IST
Considering the following code:

template <class T> struct static_abort {};

template <class E>
struct any
{
  const E& self() const { return static_cast<const E&>(*this); }
};

struct range : public any<range>
{
  range() {}

  template <class U>
  range(const U&)
  {
    typedef typename static_abort<U>::ret t;
  }
};

int main()
{
  const any<range>& r = *new range();
  r.self();
}

To convert any<range> into range, g++-3.3.1 tries to use the generic
constructor of range instead of using the subclass relationship
between them. Even if I could not find in the C++ standard a priority
rule for this particular case, previous versions of g++ (up to 3.3),
Intel c++ compiler, and Comeau c++ compiler choose the subclassing
conversion when possible.

Environment:
System: Linux ouagadougou 2.4.20 #1 Tue Jun 10 07:33:08 CEST 2003 i686 GNU/Linux
Architecture: i686

	
host: i386-pc-linux-gnu
build: i386-pc-linux-gnu
target: i386-pc-linux-gnu
configured with: ../src/configure -v --enable-languages=c,c++,java,f77,pascal,objc,ada,treelang --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-gxx-include-dir=/usr/include/c++/3.3 --enable-shared --with-system-zlib --enable-nls --without-included-gettext --enable-__cxa_atexit --enable-clocale=gnu --enable-debug --enable-java-gc=boehm --enable-java-awt=xlib --enable-objc-gc i386-linux

How-To-Repeat:
	Compile the given code.
Comment 1 nicolas.burrus@lrde.epita.fr 2003-07-04 09:26:34 IST
Fix:
	Use reinterpret_cast instead of static_cast.
Comment 2 Wolfgang Bangerth 2003-07-04 21:47:49 IST
Confirmed. This is a regression on 3.3 and 3.4, with behavior in the
branch changing somewhen after 6-22 and before now.
Comment 3 CVS Commits 2003-07-06 03:31:00 IST
Subject: Bug 11431

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	mmitchel@gcc.gnu.org	2003-07-06 03:30:57

Modified files:
	gcc/cp         : ChangeLog call.c cp-tree.h cvt.c decl.c init.c 
	                 semantics.c typeck.c typeck2.c 
	gcc/testsuite  : ChangeLog 
	gcc/testsuite/g++.old-deja/g++.jason: typeid1.C 
Added files:
	gcc/testsuite/g++.dg/expr: static_cast3.C 

Log message:
	PR c++/11431
	* typeck.c (build_static_cast): Check for reference conversions
	earlier.
	
	* cp-tree.h (perform_integral_promotions): Declare.
	* call.c (build_addr_func): Use decay_conversion.
	(convert_arg_to_ellipsis): Likewise.  Remove misleading comment.
	(convert_for_arg_passing): Use perform_integral_promotions.
	* cvt.c (build_expr_type_conversion): Use decay_conversion.
	(type_promotes_to): Do not return a cv-qualified type.
	* decl.c (grok_reference_init): Fix formatting.
	(get_atexit_node): Use decay_conversion.
	(build_enumerator): Use perform_integral_promotions.
	* init.c (build_vec_init): Use decay_conversion.
	* semantics.c (finish_expr_stmt): Likewise.
	(finish_switch_cond): Use perform_integral_promotions.
	* typeck.c (default_conversion): Likewise.
	(perform_integral_promotions): New function.
	(build_indirect_ref): Use decay_conversion.
	(build_array_ref): Use perform_integral_promotions.
	(convert_arguments): Use decay_conversion.
	(build_unary_op): Use perform_integral_promotions.
	(build_c_cast): Use decay_conversion.
	(build_modify_expr): Likewise.
	(convert_for_initialization): Likewise.
	* typeck2.c (build_x_arrow): Likewise.
	
	* g++.old-deja/g++.jason/typeid1.C: Make it a compile test, not a
	run test.
	
	PR c++/11431
	* g++.dg/expr/static_cast3.C: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.3490&r2=1.3491
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/call.c.diff?cvsroot=gcc&r1=1.400&r2=1.401
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/cp-tree.h.diff?cvsroot=gcc&r1=1.867&r2=1.868
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/cvt.c.diff?cvsroot=gcc&r1=1.140&r2=1.141
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/decl.c.diff?cvsroot=gcc&r1=1.1079&r2=1.1080
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/init.c.diff?cvsroot=gcc&r1=1.327&r2=1.328
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/semantics.c.diff?cvsroot=gcc&r1=1.319&r2=1.320
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/typeck.c.diff?cvsroot=gcc&r1=1.472&r2=1.473
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/typeck2.c.diff?cvsroot=gcc&r1=1.142&r2=1.143
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.2844&r2=1.2845
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/expr/static_cast3.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.old-deja/g++.jason/typeid1.C.diff?cvsroot=gcc&r1=1.7&r2=1.8

Comment 5 Mark Mitchell 2003-07-06 03:47:31 IST
Fixed in GCC 3.3.1 and GCC 3.4.