On 19 Nov 2003 07:44:49 -0800,
coolpint@yahoo.co.uk (CoolPint) wrote:
[color=blue]
>While I was testing my understanding of Functioin Template features by
>playing with simple function templates, I got into a problem which I
>cannot understand. I would be very grateful if someone offered me a
>kind explanation. TIA[/color]
[snip][color=blue]
>BUT if I change the template to accept const reference to T like
>below:
>template <typename T>
>const T &maximum (const T & a, const T & b)
>{
> cout << "Template Called: ";
> return a < b ? b: a;
>}
>
>#include <cstring>
>template<>
>const char * maximum<const char *>(const char * a, const char * b)
>{
> cout << "Normal called: ";
> return strcmp(a,b) < 0 ? b: a;
>}
>
>The compiler (g++ v3.2) generates the following error message.
>
>tt.cpp:25: template-id `maximum<const char*>' for `const char*
>maximum(const
> char*, const char*)' does not match any template declaration
>
>Can anyone explain what the problem is?[/color]
A specialization has to have the same arguments as the source
template, otherwise it isn't a specialization of that template.
Substituting T=const char* gives:
#include <cstring>
template<>
inline const char*& maximum<const char*>(const char*& a, const char*&
b)
{
cout << "Normal called: ";
return strcmp(a,b) < 0 ? b: a;
}
If you inline it, the reference parameters shouldn't add any overhead
(and you can define it in a header).
Tom