Topic: [Q] Template linkage?


Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/06/21
Raw View
In article fl@b17news.b17a.ingr.com, tdj@blzbub.b17c.ingr.com (Ted Johnson) writes:
>According to Stroustrup's _The C++ Programming Language_ (2d ed.),
>"template names ... do not have external linkage."
>
>1) Why?
>

A reference (page or section) would be nice -- I couldn't find that
statement. The C++ draft standard says that template names defined
in namespace scope do have external linkage, except for template
functions that are explicitly declared to have internal linkage.

>1) Does this mean that a template function must be defined in the same
>   file as it is declared?

Whatever C++PL2 actually says, you may be taking it out of context.

Some implementations require that the template implementation code be
visible at the point where an instantiation is required, some do not.
If required, you can simply include the template implementation file.

That is, some implementations allow
 template<class T> class Array { ... };
in a header "Array.h", and let you provide a separate "Array.cc" which
will be found automatically when instantiations are required.

Other implementations require that you include both "Array.h" and
"Array.cc" in any file that uses Array objects.

you have to check your compiler manual to see what it requires.
---
Steve Clamage, stephen.clamage@eng.sun.com







Author: tdj@blzbub.b17c.ingr.com (Ted Johnson)
Date: 1995/06/21
Raw View
According to Stroustrup's _The C++ Programming Language_ (2d ed.),
"template names ... do not have external linkage."

1) Why?

1) Does this mean that a template function must be defined in the same
   file as it is declared?  (This does not seem comfortable, since it
   suggests loading include files with template function definitions,
   whereas normal function definitions can be relegated to other
   "implementation" files.)  I'm using Visual C++ 2.1; is this a
   compiler-dependent phenomenon?

--
Ted Johnson               tdjohns3@ingr.com
MS IW17C1                 ph. (205) 730-7693
Intergraph Corp.          fax (205) 730-9550
Huntsville, AL 35894-0001