Topic: Template linkage


Author: mcelroy@ecst.CSUChico.EDU (James Robert McElroy)
Date: 1996/02/23
Raw View
A question about templates and linkage:

When dealing with template functions and template
classes, there are two different entities one has
to be concerned with:  the template definitions
and the template instantiations.  The definitions
are the code we write, e.g.

template <class T>
T fubar(T foo)
{
   // whatever
}

The instantiations are what the compiler generates:

a = fubar(someInt);        // compiler instantiates:

int fubar(int foo)
{
   // whatever
}

Now, there are four possible combinations for the linkage
of the definitions and instantiations:

  Definition      Instantiation

1. Internal         Internal
2. Internal         external
3. External         Internal
4. External         External

With three different compilers, I've run into three different
combinations!!!  (Default behavior, without any switches):


Template functions (non-class):

           Definition  Instantiation

1.  HP-UX C++ compiler:  External     External
2.  Borland 3.1       :  Internal     External
3.  G++               :  Internal     Internal

Template classes and template class functions:

1. HP-UX C++ compiler :  External      External
2. Borland 3.1        :  Internal      Internal
3. G++                :  Internal      Internal

HP manages to create external linkage for
instantiations through the following mechanism:

When a template function or class is first
instantiated, a directory directly off the
current directory, called "ptrepository" is
created, in which all template instantiations
are stored.  This has the peculiar effect that
once you create a template instantiation in
one program, you can  call it from another
program without ever defining it in the second
program!!!  (As long as the second program is
in the parent directory of "ptrepository").

Has  the ANSI C++ committee come to any sort of
agreement on the linkage of template definitions
and instantiations?
--
Jim McElroy
Calif. State Univ., Chico
mcelroy@ecst.csuchico.edu
---
[ To submit articles: Try just posting with your newsreader.  If that fails,
                      use mailto:std-c++@ncar.ucar.edu
  FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]