Thread: [Dev-C++] template and prototype
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <h.r...@gm...> - 2002-03-03 00:36:35
|
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.
Thanks in advance
Horst
--
--------------------------
Horst Rößner
Postf. 1802
52205 Stolberg
02402 / 6178
0171 6455636
GMX - Die Kommunikationsplattform im Internet.
http://www.gmx.net
|
|
From: <h.r...@gm...> - 2002-03-03 01:24:07
|
> > -----Original Message-----
> > 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
Hi Ioannis,
many thanks for the quick answer.
I followed your tip but the linker means:
3 hr_math.cpp
warning: keyword 'export' not implemented and will be ignored
Horst
PS: sorry Ioannis for the PM
--
--------------------------
Horst Rößner
Postf. 1802
52205 Stolberg
02402 / 6178
0171 6455636
GMX - Die Kommunikationsplattform im Internet.
http://www.gmx.net
|
|
From: Ioannis V. <no...@ya...> - 2002-03-03 01:44:04
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of h.r...@gm... > Sent: Sunday, March 03, 2002 3:24 AM > To: dev...@li... > Subject: [Dev-C++] template and prototype > > Hi Ioannis, > many thanks for the quick answer. > I followed your tip but the linker means: > > 3 hr_math.cpp > warning: keyword 'export' not implemented and will be ignored > > Horst > > PS: sorry Ioannis for the PM > If this is only a warning and the exe is created and it works, ignore it. Its meaning is obvious. However for best results You had better download the latest MINGW32 release and replace the one that comes with Dev-C++. It is high time a new interim release of Dev-C++ to be released having the most recent version of MINGGW32. Colin are you there? :) Download the latest MINGW32 by clicking here: http://prdownloads.sourceforge.net/mingw/MinGW-1.1.tar.gz (release 2001-09-20 05:00) . However you must be able to untar/gunzip the contents. One program that does this in Windows is Winrar. This link is from page: http://sourceforge.net/project/showfiles.php?group_id=2435 Official MINGW site: http://www.mingw.org Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.d2g.com * Alternative URL: http://run.to/noicys |
|
From: <h.r...@gm...> - 2002-03-03 01:57:38
|
> > -----Original Message----- > > Subject: [Dev-C++] template and prototype > > > > Hi Ioannis, > > many thanks for the quick answer. > > I followed your tip but the linker means: > > > > 3 hr_math.cpp > > warning: keyword 'export' not implemented and will be ignored > > > > Horst > > > > PS: sorry Ioannis for the PM > > > > > If this is only a warning and the exe is created and it works, ignore > it. Its meaning is obvious. However for best results You had better > download the latest MINGW32 release and replace the one that comes with > Dev-C++. It is high time a new interim release of Dev-C++ to be released > having the most recent version of MINGGW32. Colin are you there? :) > > Download the latest MINGW32 by clicking here: > http://prdownloads.sourceforge.net/mingw/MinGW-1.1.tar.gz (release > 2001-09-20 05:00) . > > > However you must be able to untar/gunzip the contents. One program that > does this in Windows is Winrar. > > > This link is from page: > http://sourceforge.net/project/showfiles.php?group_id=2435 > > Official MINGW site: http://www.mingw.org > > > > Ioannis all of that I have done ( some days ago ) It is namely a warning, but no exe is created. Horst -- -------------------------- Horst Rößner Postf. 1802 52205 Stolberg 02402 / 6178 0171 6455636 GMX - Die Kommunikationsplattform im Internet. http://www.gmx.net |
|
From: Ioannis V. <no...@ya...> - 2002-03-03 02:25:16
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of h.r...@gm... > Sent: Sunday, March 03, 2002 3:58 AM > To: Dev...@li... > Subject: RE: [Dev-C++] template and prototype > > > all of that I have done ( some days ago ) > It is namely a warning, but no exe is created. Indeed! I just checked BC++ and they have not implemented export too (the compiler ignores it). I knew its existence from theory (TC++PL Special Edition). Till it gets implemented you can do one of the following: a) Provide the definition at every compilation unit, possibly in a header file. b) Do not use templates but overloaded functions. This implies one definition for anything making it a mess to maintain. Also in overloading there may take place implicit type conversions without any warning and that may be bad. Do not follow option b. :) Follow option a and be patient till it gets implemented. The official C++ standard is very recent, and the extent in which it has been implemented is magnificent for such a small period and such a big size. Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.d2g.com * Alternative URL: http://run.to/noicys |
|
From: <h.r...@gm...> - 2002-03-03 03:04:47
|
> > -----Original Message----- > > Subject: RE: [Dev-C++] template and prototype > > > > > > > all of that I have done ( some days ago ) > > It is namely a warning, but no exe is created. > > > Indeed! I just checked BC++ and they have not implemented export too > (the compiler ignores it). I knew its existence from theory (TC++PL > Special Edition). Till it gets implemented you can do one of the > following: > > a) Provide the definition at every compilation unit, possibly in a > header file. > > b) Do not use templates but overloaded functions. This implies one > definition for anything making it a mess to maintain. Also in > overloading there may take place implicit type conversions without any > warning and that may be bad. > > Do not follow option b. :) Follow option a and be patient till it gets > implemented. The official C++ standard is very recent, and the extent in > which it has been implemented is magnificent for such a small period and > such a big size. > > > > Ioannis after your hint, I read something. In ARM (1995) export is not mentioned. In Stroustrup, The C++ ... (1998) there is a whole chapter about template, export etc. Is that what you mean with TC++PL ? Again many thanks I will choose option a. placing the definition in one Header and include it. Horst -- -------------------------- Horst Rößner Postf. 1802 52205 Stolberg 02402 / 6178 0171 6455636 GMX - Die Kommunikationsplattform im Internet. http://www.gmx.net |
|
From: Ioannis V. <no...@ya...> - 2002-03-03 04:57:17
|
> > after your hint, I read something. In ARM (1995) export is > not mentioned. ARM is considered obsolete. > In Stroustrup, The C++ ... (1998) there is a > whole chapter about template, export etc. Is that what you > mean with TC++PL ? Again many thanks Yes. "The C++ Programming Language" 3rd Edition or Special Edition. > I will choose option a. placing the definition in one Header > and include it. Good. Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.d2g.com * Alternative URL: http://run.to/noicys |
|
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
|