This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/11431] New: static_cast behavior with subclasses when default constructor available
- From: "gcc-bugzilla at gcc dot gnu dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 4 Jul 2003 09:26:37 -0000
- Subject: [Bug c++/11431] New: static_cast behavior with subclasses when default constructor available
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11431
Summary: static_cast behavior with subclasses when default
constructor available
Product: gcc
Version: 3.3.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: nicolas dot burrus at lrde dot epita dot fr
CC: gcc-bugs at gcc dot gnu dot org
GCC build triplet: i386-pc-linux-gnu
GCC host triplet: i386-pc-linux-gnu
GCC target triplet: i386-pc-linux-gnu
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.
------- Additional Comments From nicolas dot burrus at lrde dot epita dot fr 2003-07-04 09:26 -------
Fix:
Use reinterpret_cast instead of static_cast.