Browse Prior Art Database

C++ Compiler Implementation for Virtual Functions with Varying Return Types

IP.com Disclosure Number: IPCOM000115849D
Original Publication Date: 1995-Jun-01
Included in the Prior Art Database: 2005-Mar-30
Document File: 5 page(s) / 153K

Publishing Venue

IBM

Related People

Choi, EA: AUTHOR

Abstract

An implementation for supporting C++ contravariant virtual functions is described. Secondary entry points within the target function enable the function to determine the adjustment values that would be applied to the return value just before the function returns.

This text was extracted from an ASCII text file.
This is the abbreviated version, containing approximately 30% of the total text.

C++  Compiler Implementation for Virtual Functions with Varying Return
Types

      An implementation for supporting C++  contravariant virtual
functions is described.  Secondary entry points within the target
function enable the function to determine the adjustment values that
would be applied to the return value just before the function
returns.

      In a C++  compiler implementation, an overriding virtual
function would normally have its address stored in the same virtual
function table entry that its overridden virtual function had
occupied.  This is fine if the overriding virtual function returns
exactly the same type as the overridden virtual function.

      However, the overriding virtual function may also return a type
that differs from the return type of the overridden function.  Such
an overriding virtual function is called a "contravariant" function.
(In C++, a virtual function may have contravariant overrides only if
it returns a pointer (or reference) to a class.  When "virtual
function" is mentioned in the remainder of this article, it will be
assumed that the function returns a pointer (or reference) to a
class.)

      If the overridden virtual function returns a pointer (or
reference) to a class T then a problem may arise if the overriding
virtual function returns a pointer (or reference) to a class D
derived
from T and D has multiple base classes or has virtual bases.  In such
a case, the pointer (or reference) that the overriding function
returns
may not be usable as a pointer (or reference) to T without
alteration.

      The proposed solution is to implement the overriding virtual
function as a function with two entry points.  At each entry point, a
"return value adjustment" is stored in a local variable returnAdjust.
Just before the function returns, it will add the contents of
returnAdjust to the return value, thereby adjusting the pointer (or
reference) to the proper type.

      The main entry point will be associated with a new virtual
function table entry since it will return a new type.  The second
entry point will return the same type that the overridden virtual
function returns and hence its address will be stored in the same
virtual function table entry as the entry that was occupied by the
overridden virtual function.

      The main entry point will store zero into returnAdjust  so that
the return type associated with the overriding virtual function's
declaration will be obtained.  The other entry point will store, into
returnAdjust, the offset of the base class T  within an object of
class D.

      In general, an overriding virtual function may override more
than one virtual function and its return type may differ from each
one of them.  This implies that the overriding virtual function may
require an additional entry point for each overridden virtual
function.  Each entry point will store the appropriate offset
(between the overridden function's type and the...