www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/04/26/18:31:09

Date: Wed, 26 Apr 1995 14:31:47 -0700 (PDT)
From: Gordon Hogenson <ghogenso AT u DOT washington DOT edu>
To: "B.S.Runnacles" <B DOT S DOT Runnacles AT soton DOT ac DOT uk>
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: Re: Template problems

On Tue, 25 Apr 1995, B.S.Runnacles wrote:

> Dear All
> 
> I'm having a few problems understanding what exactly to do with templates.
> I think I adopt a rather non-standard coding style, in that all my C++ code
> is kept in header files with extensions .hxx, and class definitions in files
> .h.  I use templates to provide type-independant linked lists, queues etc.
> The problem comes when I want to use two lists of the same type in different
> source files, say two lists of chars.  When I do this, the linker complains
> that I have mutiple references to LinkedList <char>.  
> 

The code for the template is getting generated in both files.  To prevent this,
compile with -fno-implicit-templates and include the following line in one of
your .cc files:

template class LinkedList<char>;

which will cause the char version of LinkedList to be generated only in that file.
The only drawback of this method is that you have to have a line like this
for *every* template that you use.  

If this is an excessive pain, you could have *one* file that you compile without
-fno-implicit-templates.  In this file you either use all your template classes
(i.e. use all conceivable LinkedLists that appear in your program) or put in
explicit instantiations for them (as above) if you don't happen to use them
in that file.

(You can achieve the same effect via -fexternal-templates but this is somewhat
uglier as it requires the use of #pragmas).

Word on the street is that this will be fixed in the next version (2.7) so
that things work more or less automagically.
--
Gordon J. Hogenson <ghogenso AT u DOT washington DOT edu>         Tel. (206) 685-2951

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019