BUG: covariant return types

R

Rob Grainger

Hi,

When compiling the following code under VC .NET,

template <typename T>
class IBaseCollection
{
public:
virtual T Get() const = 0;
};

template <typename T, typename Base>
class IDerivedCollection : public Base
{
public:
virtual T Get() const = 0;
};

class Element
{
public:
virtual Element* Get() = 0;
virtual IBaseCollection<Element*>& GetCollection() = 0;
};

class OwnedElement : public Element
{
public:
virtual OwnedElement* Get() = 0;
virtual IDerivedCollection<OwnedElement*, IBaseCollection<Element*> >&
GetCollection() = 0;
};

I receive the following compile error:

c:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\CollTest\CollTest.cpp(19) : error C2555:
'IDerivedCollection<T,Base>::Get': overriding virtual function return type
differs and is not covariant from 'IBaseCollection<T>::Get'
with
[
T=OwnedElement *,
Base=IBaseCollection<Element *>
]
and
[
T=Element *
]
c:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\CollTest\CollTest.cpp(11) : see declaration of
'IBaseCollection<T>::Get'
with
[
T=Element *
]
c:\Documents and Settings\Administrator\My Documents\Visual Studio
Projects\CollTest\CollTest.cpp(35) : see reference to class template
instantiation 'IDerivedCollection<T,Base>' being compiled
with
[
T=OwnedElement *,
Base=IBaseCollection<Element *>
]

In fact, the return type of Get with the parameters given is Element* for
IBaseCollection<Element*> and OwnedElement*
for IDerivedCollection<OwnedElement*, IBaseCollection<Element*>. According
to ANSI C++, these should be covaraint types.

Does anyone know any work arounds, or something I'm missing here,

Thanks,

Rob
 
C

Carl Daniel [VC++ MVP]

Rob said:
Hi,

When compiling the following code under VC .NET,

That does appear to be a bug in VC7.1 (I didn't test VC7, but presumably
it's a bug in that version as well).

It appears to be fixed in the Whidbey (VC8) alpha.

-cd
 
R

Rob Grainger

Carl,

Thanks for your prompt response - at least I know to find another route..

Incidentally, will VC8 (or does VC 7.1) also support partial template
specializations ?

Rob
 
C

Carl Daniel [VC++ MVP]

Rob said:
Carl,

Thanks for your prompt response - at least I know to find another
route..

Incidentally, will VC8 (or does VC 7.1) also support partial template
specializations ?

VC7.1 supports PTS.

-cd
 
R

Rob Grainger

Carl,

Somewhat delayed (I don't get round to using newsgroups as often as I'd
like, due to work constraints) but I have found a workaround, which is to
use Comeau's excellent little C++ compiler - as a front-end for MSVC, this
gives me that covariant return, which proved crucial to my application (at
least to avoid horrible, un-typesafe Collection<Object*> style stuff).

Bit of a pain for IDE integration, but reckon I can hold out until the
compiler catches up.. this is purely for a personal project, so I'll keep
using VC for other projects.

Thanks for the help. Is there any online reference to compliance notes with
ANSI C++ for VC versions. This would be a definite help..

Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top