RE: [Dev-C++] template and prototype
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ya...> - 2002-03-03 01:04:16
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of h.r...@gm...
> Sent: Sunday, March 03, 2002 2:36 AM
> To: dev...@li...
> Subject: [Dev-C++] template and prototype
>
>
> Hello,
>
> Is it forbidden to have a prototype of a template in a header file ?
> For example:
>
> a.h
> template <class T> T sqr(T x);
>
> a.cpp
> template <class T> T sqr(T x) { return x * x; }
>
> this doesnt link.
> If I put the definition
> template <class T> T sqr(T x) { return x * x; }
> in the header, it works.
If you want to place the definition in one file and use declarations to
the rest, you must place the keyword export in the template definition
in the style:
export template <class T>
T sqr(T x) { return x*x; }
Then you can use the declaration in the way you used it to access the
template from other translation units. The export is necessary to make
it possible someone to make a compiler that knows what is going on. :)
If you do not like export you must provide the template definition in
every unit you use it (templates have by default local scope in contrast
to ordinary functions).
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.d2g.com
* Alternative URL: http://run.to/noicys
|