Thread View: comp.std.c++
2 messages
2 total messages
Started by Marian Hellema
Tue, 02 Apr 1996 13:34
Syntax for template instantiation and specialization
Author: Marian Hellema
Date: Tue, 02 Apr 1996 13:34
Date: Tue, 02 Apr 1996 13:34
40 lines
1930 bytes
1930 bytes
Hello,
What is the correct syntax for template instantiation and
specialization?
From my copy of the DWP April 95 I gather:
template<class T>
T max(T a, T b) { return a>b?a:b; }
template int max<int>(int, int); //instantiation
/* OR: */
template int max<>(int, int); //instantiation
const char* max<const char*>max(const char* a, const char* b)
{ /* specialization */ }
/* OR: */
const char* max<>(const char* a, const char* b)
{ /* specialization */ }
My questions are:
1. Is the above correct? I heard there are some recent changes
in this area.
2. Is the following still legal:
int max(int, int); //instantiation??
Thanks!
Marian
--
----------------------------------------------------------------------
Marian Hellema AT Computing, UNIX training and consultancy
email: marian@atcmp.nl P.O. Box 1428, 6501 BK Nijmegen
phone: +31 24 3527225 the Netherlands
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Re: Syntax for template instantiation and specialization
Author: David Vandevoorde
Date: Wed, 03 Apr 1996 17:23
Date: Wed, 03 Apr 1996 17:23
61 lines
2630 bytes
2630 bytes
>>>>> "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
]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads