User account creation filtered due to spam.

Bug 11431 - static_cast behavior with subclasses when default constructor available
Summary: static_cast behavior with subclasses when default constructor available
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
Depends on:
Reported: 2003-07-04 09:26 UTC by
Modified: 2003-07-06 03:47 UTC (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


Note You need to log in before you can comment on or make changes to this bug.
Description 2003-07-04 09:26:34 UTC
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();

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.

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

	Compile the given code.
Comment 1 2003-07-04 09:26:34 UTC
	Use reinterpret_cast instead of static_cast.
Comment 2 Wolfgang Bangerth 2003-07-04 21:47:49 UTC
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 UTC
Subject: Bug 11431

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	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
	* 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.


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