Topic: Syntax for template instantiation and specialization


Author: Marian Hellema <marian@atcmp.nl>
Date: 1996/04/02
Raw View
Hello,

What is the correct syntax for template instantiation and
specialization?



Author: vandevod@cs.rpi.edu (David Vandevoorde)
Date: 1996/04/03
Raw View
>>>>> "MH" == Marian Hellema <marian@atcmp.nl> writes:
MH> What is the correct syntax for template instantiation and
MH> specialization?

MH> From my copy of the DWP April 95 I gather:

This has changed somewhat since then...

MH> template<class T>
MH> T max(T a, T b) { return a>b?a:b; }

MH> template int max<int>(int, int); //instantiation
MH> /* OR: */
MH> template int max<>(int, int);  //instantiation

(or you can leave out the `<>', I think).

MH> const char* max<const char*>max(const char* a, const char* b)
MH> {  /* specialization */ }

Should now be:

 template<> // <- note this!
 char const*
 max<char const*>(char const*, char const*) // ...

MH> const char* max<>(const char* a, const char* b)
MH> {  /* specialization */ }

Same thing...

 template<> // <- note this!
 char const*
 max(char const*, char const*) // ...

should be OK. I believe `max<>' would be fine too.
(I'm deducing this from _examples_ in a not-yet-WP rework of the
templates-chapter; I could very well be very wrong).

[...]
MH> 2. Is the following still legal:
MH>    int max(int, int); //instantiation??

It is still legal and I believe this is what would be called a
``guiding function declaration''. Without the guiding declaration

 max('a', 100);

would not find a match (because conversions are not considered when
matching template arguments); _with_ the guiding declaration, the
regular overloading rules (possibly involving implicit conversions)
apply.

 Daveed
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]