Topic: Template function inside a non-template class


Author: David R Tribble <david.tribble@central.beasys.com>
Date: 1998/02/10
Raw View
Martin, Connell@netlab.cs.rpi.edu wrote:
> As I understand it, using template members in VC++5.0 is really
> pushing the compiler.  My experience is that sometimes they work,
> sometimes they don't.  Unless I have missed it, there is no official
> documentation to say that the compiler supports template members.

And good luck trying to use templates with DLLs (dynamic libraries).
It's caused us no end of grief trying to get our code, which
compiles just fine on several Unix systems (even with older
compilers), to compile on Win/NT.  Throw in inheritance, virtual
methods, and inline functions and it can get truly annoying.
VC++ may support more of the language right now, but it's in the
dark ages when it comes to building libraries.  Be prepared to
change every source file.

Microsoft should hire someone who knows how to write a real linker,
so we wouldn't need all that '__declspec(dllexport)' gibberish.

I predict that full template support amongst the various compiler
vendors will take about two years, based on our experience of how
long it took full C89 support to catch on.  In the mean time, just
grit your teeth and don't use too many of the newer features of
the language (which includes a lot of the rather nice but advanced
template features).

-- David R. Tribble, david.tribble@noSPAM.central.beasys.com --

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Gerhard Menzl <gerhard.menzl@sea.ericsson.removethistomail.se>
Date: 1998/02/11
Raw View
David R Tribble wrote:

> And good luck trying to use templates with DLLs (dynamic libraries).
> It's caused us no end of grief trying to get our code, which
> compiles just fine on several Unix systems (even with older
> compilers), to compile on Win/NT.  Throw in inheritance, virtual
> methods, and inline functions and it can get truly annoying.
> VC++ may support more of the language right now, but it's in the
> dark ages when it comes to building libraries.  Be prepared to
> change every source file.

I hope that this applies only to inheritance/instantiation relationships
*across DLL boundaries*.

> Microsoft should hire someone who knows how to write a real linker,
> so we wouldn't need all that '__declspec(dllexport)' gibberish.

An expert on object-oriented design or two wouldn't hurt either. <g>

Gerhard Menzl


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Manfred Knemeyer" <nospam@ix.netcom.com>
Date: 1998/02/07
Raw View

Frank wrote in message <6bebij$ar5@netlab.cs.rpi.edu>...
>I'm having problems getting a template function to work as a member
>function in a class.  Does anyone know if this is at all possible.
>
--- snip ---
>----------------------- Get an Error
>01: class TemplateTest
>02: {
>03: public:
>04:   template<class T, class U>
>05:   T implicit_cast(U u)
>06:   { return u; }
>07: };
--- snip ---

Template class should be parameterized.
Otherwise, when it is instantiated, where
would these parameters come from to be
used in declaring members?


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Martin Connell"@netlab.cs.rpi.edu
Date: 1998/02/09
Raw View
As I understand it, using template members in VC++5.0 is really pushing
the compiler.  My experience is that sometimes they work, sometimes
they don't.  Unless I have missed it, there is no official
documentation to say that the compiler supports template members.  The
implementation certainly isn't complete.  Searching through the MSDN
for "member template" comes up with about three docs.  When it does
work, the debugger symbol information is very often incomplete.  My
experience has been that templatized constructors seem to work well.
Templatized member functions, on the other hand, can very often only be
called by other members: trying to call them from outside the class
causes one of those unrecoverable compiler errors.

Regards
Martin Connell

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Frank <feaves@qualcomm.com>
Date: 1998/02/06
Raw View
I'm having problems getting a template function to work as a member
function in a class.  Does anyone know if this is at all possible.

Here is an example of what I'm trying to do:

--------------------------- Works Fine
-----------------------------------
template<class T, class U>
T implicit_cast(U u)
{ return u; }

void Func()
{
  int i;
  double d;

  d = TemplateTest::implicit_cast<double>(i);
}
------------------------------------------------------------------------
---

----------------------- Get an Error
-------------------------------------
01: class TemplateTest
02: {
03: public:
04:   template<class T, class U>
05:   T implicit_cast(U u)
06:   { return u; }
07: };
08:
09: template<class T, class U>
10: T implicit_cast(U u)
11: { return u; }
12:
13: void Func()
14: {
15:   int i;
16:   double d;
17:   TemplateTest Test;
18:
19:   d = implicit_cast<double>(i);
20:   d = Test.implicit_cast<double>(i);
21: }
------------------------------------------------------------------------
---

What's weird is that line #19 works fine, but I get an error on line
#20, error message, "type 'double' unexpected".
The compiler that I'm using is Microsoft Visual C++ 5.0.

I do know that the following code will work...
-------------------------- Not the solution I'm looking for
----------------
00: template<class T>
01: class TemplateTest
02: {
03: public:
04:   template<class U>
05:   T implicit_cast(U u)
06:   { return u; }
07: };
08:
09: template<class T, class U>
10: T implicit_cast(U u)
11: { return u; }
12:
13: void Func()
14: {
15:   int i;
16:   double d;
17:   TemplateTest<double> Test;
18:
19:   d = implicit_cast<double>(i);
20:   d = Test.implicit_cast(i);
21: }
------------------------------------------------------------------------
-----
However, I want to have a template member function inside a non-template
class.

Any help would be greatly appreciated.

Thanks,

Frank

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]