Re: Covariant returns

Zalman Stern <zalman@netcom11.netcom.com>
28 Nov 1999 01:37:11 -0500

          From comp.compilers

Related articles
Covariant returns lakshman@sasi.com (Lakshman KNVSM) (1999-11-23)
Re: Covariant returns zalman@netcom11.netcom.com (Zalman Stern) (1999-11-28)
| List of all articles for this month |

From: Zalman Stern <zalman@netcom11.netcom.com>
Newsgroups: comp.compilers
Date: 28 Nov 1999 01:37:11 -0500
Organization: MindSpring Enterprises
References: 99-11-131
Keywords: C++, code

Lakshman KNVSM <lakshman@sasi.com> wrote:
: Hi,
: How are covariant returns implemented in C++? I mean, how is the pointer
: adjustment required done in case of Multiple Inheritance?


: [This came up a couple of days ago. Either with function wrappers (often
: incorrectly called thunks) or with an adjustment field in the vtbl. -John]


I'm not sure I understand John's comment. I wouldn't expect function
wrappers to be generated for covariant return types.


        class base {
virtual base *f();
        };


        class derived : public base {
#if COVARIANT_RETURNS // For illustrative purposes only...
virtual derived *f();
#else
virtual base *f();
#endif
        };




The programmer must provide two definitions of f(), base::f() and
derived::f() with appropriate return types. The compiler will not
automatically generate a derived::f(). In the case of multiple
inheritance and covariant return types, I would expect the compiler to
generate code such that member functions always returns a pointer to
the topmost base class that defines the member function (as they
always have right?) and then when the member function is called,
generate code to apply any needed offset right there. Adding covariant
return types to the language allows this to happen in a safe static
typed way without writing explicit casts.


So given the above class declarations, one writes:
derived *foo = ...;
#if COVARIANT_RETURNS
derived *bar = foo->f();
#else
derived *bar = static_cast<derived *>(foo->f());
#endif


I'd expect the same code to be generated whether COVARIANT_RETURNS is true
or not. But perhaps I'm missing something.


-Z-


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.