[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

static_cast behavior with subclasses when default constructor available



>Submitter-Id:	net
>Originator:	Nicolas Burrus
>Organization:	LRDE
>Confidential:	no
>Synopsis:	static_cast behavior with subclasses when default constructor available
>Severity:	serious
>Priority:	medium
>Category:	c++
>Class:		rejects-legal
>Release:	3.3.1 20030626 (Debian prerelease) (Debian testing/unstable)
>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
>Description:

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.

>How-To-Repeat:
	Compile the given code.
>Fix:
	Use reinterpret_cast instead of static_cast.



Reply to: