We've been developing an app under gcc-3.4, and
were pleased with how strictly gcc-3.4 enforced
the C++ standard :-)
But in porting to another compiler, we discovered
that gcc had let us use a nonportable construct:
Types like void*, int*, and float* are not covariant,
but gcc happily lets subclasses and superclasses
use them interchangably for return types.
The following example demonstrates the problem; it
compiles ok on gcc even with -W -Wall, but not on
ICC 8.1 or Comeau's online compiler:
typedef int type1;
typedef float type2;
class A {
virtual ~A() {};
virtual void * result() const = 0;
};
class B : public A {
virtual type1 * result() const { return new type1; };
};
class C : public A {
virtual type2 * result() const { return new type2; };
};
ICC 8.1 Errors:
Intel(R) C++ Compiler for 32-bit applications, Version 8.1 Build 20040921Z
Package ID: l_cc_pc_8.1.022
test-covarient.cc(14): error: return type is not identical to nor covariant with
return type "void *" of overridden virtual function function "A::result"
virtual type1 * result() const { return new type1; };
^
test-covarient.cc(18): error: return type is not identical to nor covariant with
return type "void *" of overridden virtual function function "A::result"
virtual type2 * result() const { return new type2; };
Comeau Errors:
Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++
"ComeauTest.c", line 10: error: return type is not identical to nor covariant with
return type "void *" of overridden virtual function function
"A::result"
virtual type1 * result() const { return new type1; };
^
"ComeauTest.c", line 14: error: return type is not identical to nor covariant with
return type "void *" of overridden virtual function function
"A::result"
virtual type2 * result() const { return new type2; };
--
Summary: gcc lets subclasses use noncovariant return types
Product: gcc
Version: 3.4.1
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: joshh at google dot com
CC: gcc-bugs at gcc dot gnu dot org
GCC build triplet: i686-unknown-linux-gnu
GCC host triplet: i686-unknown-linux-gnu
GCC target triplet: i686-unknown-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18064