473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

BUG: covariant return types

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
Nov 16 '05 #1
4 1214
Rob Grainger wrote:
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

Nov 16 '05 #2
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

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:us**************@tk2msftngp13.phx.gbl...
Rob Grainger wrote:
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

Nov 16 '05 #3
Rob Grainger wrote:
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
Nov 16 '05 #4
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

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:uH**************@TK2MSFTNGP11.phx.gbl...
Rob Grainger wrote:
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

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alex Vinokur | last post by:
Hello, Here is some program with virtual constructors. Is there any difference between * clone1() vs. clone2() * create1() vs. create2() ? It seems that it should be.
3
by: Sankar Nemani | last post by:
Hi, Does anyone know what the reason behind not allowing to use "covariant return types when overriding methods in derived classes" in C# is? Also while other OO languages such as Java, don't...
14
by: Stefan Slapeta | last post by:
Hi, this code does not compile in C#: class base_class {} class derived_class : base_class {} class A { public virtual base_class f()
13
by: Stephen Walch | last post by:
Error C2392 is hitting me hard! I have a managed C++ library that implements a bunch of fixed interfaces. For example, one interface is: public abstract interface IDbCommand { public...
2
by: Mike | last post by:
I keep running into the scenario below over and over again. Currently I get around not having covariant return types by using an interface and explicit property definitions which works to some...
16
by: Bob Hairgrove | last post by:
Consider the classic clone() function: class A { public: virtual ~A() {} virtual A* clone() const = 0; }; class B : public A { public:
6
by: miked | last post by:
Why are there still no covariant return types? All searches reveal no workarounds accept for using an interface which is a real pain. I wind up missing this capability almost every time I...
8
by: Alex Vinokur | last post by:
Here is a code from http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8 -------------------------------------- class Shape { public: virtual ~Shape() { } // A...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
9
by: Rahul | last post by:
Hi Everyone, I was trying to implement covariant return types and i get a compilation error, class BB : public AA { }; class A
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.