Browse Prior Art Database

Overridable Multiply Defined Symbols for C++ Template Resolution

IP.com Disclosure Number: IPCOM000116460D
Original Publication Date: 1995-Sep-01
Included in the Prior Art Database: 2005-Mar-30
Document File: 2 page(s) / 71K

Publishing Venue

IBM

Related People

Kielstra, AH: AUTHOR [+4]

Abstract

A method for resolving multiple definitions of template generated functions is disclosed. The method removes the unselected function bodies and does not require the use of a prelink step.

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

Overridable Multiply Defined Symbols for C++ Template Resolution

      A method for resolving multiple definitions of template
generated functions is disclosed.  The method removes the unselected
function bodies and does not require the use of a prelink step.

      Templates are a C++ language feature that act like automatic
macros that the compiler uses to define classes, functions, and some
types of data.  A definition created by expanding a template will be
used only if the programmer has not already explicitly supplied a
definition of the item in question.

The following exemplifies the template mechanism as it applies to
functions:
  template<class T>  int foo(T t)  { return t; }
  void foo(int w)  { return w + 1; }
  main() {
    int i = foo(1.0);
    int j = foo(1);
  }

      This program calls two versions of the "foo" function, one
which accepts a floating point value as a parameter, and one which
accepts an integer.  The compiler will expand the template,
substituting the "float" type for "T", to generate a definition of
foo(float), but will recognize that the programmer has supplied an
explicit definition of foo(int).  The explicit definition will be
used for the call to foo(1), rather than expanding the template
again.

      This is easily implemented within a single compilation, but is
more difficult in an application that consists of several independent
compile units.  In that case, the compiler must arrange to use an
explicit user-supplied definition, if one exists, in any of the
compile units.  But, if no explicit definition is found anywhere,
then an expansion of the template should be used instead.

      In the above example, if an explicit definition of foo(float)
exists in any other compile unit bound with this one, the compiler
must somehow detect this fact, discard all template expansions it has
already generated, and use the explicit definition. ...