Article View: alt.linux
Article #1090Re: Templates
From: RA Scheltema
Date: Thu, 08 Apr 1999 00:00
Date: Thu, 08 Apr 1999 00:00
59 lines
3000 bytes
3000 bytes
Templates are implemented in kind of a funny way. The compiler has to know
on compile-time which instances of the template it is going to use. For
example:
template<class T>
class Array {
public:
Array(int size);
~Array();
private:
T *a;
};
#include "Array.cpp"
void main() {
Array<char> chars;
Array<int> ints;
}
The compiler makes two instances of the Array-class in this example (one for
the chars and one for the ints). In both instances proprietary code is added
for the template T to make the thing work. In order to do this the compiler
has to know how the functions are implemented. When you would have made
object-files (normal procedure) the compiler doesn't know how to do the
replace, since it doesn't know anything when you're making the object-files.
The above example shows the, more or less, right way to work with templates
(STL for example doesn't work like this, but that's spaghetti-code). Just
make a header and source-file in the normal way, but include the source-file
in the header-file and don't make an object-file.
Hope this helps.
Richard Scheltema
Steven D. Nakhla wrote in message ...
>I'm trying to design template classes in C++ (under Linux and Solaris)
using
>the gcc compiler, but I'm running into trouble. It seems that the template
>functions must be written
>within the header (.h) file. Whenever they are written in the .cpp file I
>get errors about them not being there. Is there any way around this? Can
I
>send a flag to the compiler or anything to get them implemented correctly?
>
>
>Please reply to:
>
>Steve Nakhla
>nakhla@bcpl.net
>
>
>
>
Technical Details
Message-ID:
<370c5137.0@pandora.research.kpn.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<ShVO2.260$G4.285045@news.abs.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f5392,eaa6e2f181a1fe60 X-Google-Attributes: gidf5392,public X-Google-Thread: 10c696,eaa6e2f181a1fe60 X-Google-Attributes: gid10c696,public X-Google-Thread: f8c9b,eaa6e2f181a1fe60 X-Google-Attributes: gidf8c9b,public X-Google-Thread: fc89c,eaa6e2f181a1fe60 X-Google-Attributes: gidfc89c,public X-Google-Thread: faa64,eaa6e2f181a1fe60 X-Google-Attributes: gidfaa64,public X-Google-Thread: 109fba,eaa6e2f181a1fe60 X-Google-Attributes: gid109fba,public X-Google-Thread: 100c5f,eaa6e2f181a1fe60 X-Google-Attributes: gid100c5f,public X-Google-Thread: 10ba33,eaa6e2f181a1fe60 X-Google-Attributes: gid10ba33,public From: "RA Scheltema" <scheltem@kpn.research.com> Subject: Re: Templates Date: 1999/04/08 Message-ID: <370c5137.0@pandora.research.kpn.com> X-Deja-AN: 463940336 References: <ShVO2.260$G4.285045@news.abs.net> X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4 X-Complaints-To: usenet@news.surfnet.nl X-Trace: news.surfnet.nl 923554106 4719 139.63.192.11 (8 Apr 1999 06:48:26 GMT) Organization: KPN Research NNTP-Posting-Date: 8 Apr 1999 06:48:26 GMT Newsgroups: alt.linux,alt.os.linux,comp.lang.c++,comp.os.linux.development.apps,comp.os.linux.development.system,comp.unix.programmer,comp.unix.questions,gnu.g++.help,gnu.gcc.help