![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
Function Template Question
Assuming I have the following code below.
template <class T> T maxn(T num[], int n)
{
T largest = num[0];
while (n-- > 0)
{
if (num[n] > largest)
largest = num[n];
}
return largest;
}
template <> char * maxn(char *string[], int n)
{
char *longest = string[0];
while (n-- > 0)
{
if (strlen(string[n]) > strlen(longest))
longest = string[n];
}
return longest;
}Why do I really need the template <> declaration in front of the second function to say it's a specialization? If I take out the template <> it still compiles and works the same. Can someone clarify this for me? |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Without the "template <>" syntax, it is not a template specialisation. It is an overloaded function. Compare the behaviour of this code snippet;
template <class T> T maxn(T num[], int n)
{
T largest = num[0];
while (n-- > 0)
{
if (num[n] > largest)
largest = num[n];
}
return largest;
}
char * maxn(char *string[], int n); // function defined elsewhere
int main()
{
char *x[] = {"A", "B", "C"};
maxn(x, 3); // will not call a specialisation of the template
maxn<char *>(x, 3); // will call a specialisation of the template
}template <class T> T maxn(T num[], int n)
{
T largest = num[0];
while (n-- > 0)
{
if (num[n] > largest)
largest = num[n];
}
return largest;
}
template <> char * maxn(char *string[], int n)
{
char *longest = string[0];
while (n-- > 0)
{
if (strlen(string[n]) > strlen(longest))
longest = string[n];
}
return longest;
}
int main()
{
char *x[] = {"A", "B", "C"};
maxn(x, 3); // will call a specialisation of the template
maxn<char *>(x, 3); // will call the same specialisation of the template
}template <class T> T maxn(T num[], int n)
{
T largest = num[0];
while (n-- > 0)
{
if (num[n] > largest)
largest = num[n];
}
return largest;
}
template <> char * maxn(char *string[], int n)
{
char *longest = string[0];
while (n-- > 0)
{
if (strlen(string[n]) > strlen(longest))
longest = string[n];
}
return longest;
}
char * maxn(char *string[], int n); // function defined elsewhere
int main()
{
char *x[] = {"A", "B", "C"};
maxn(x, 3); // ambiguous (the template specialisation
// and external function are both suitable candidates)
maxn<char *>(x, 3); // will call a specialisation of the template
} |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
Then I guess the better question is what's the difference between making it a template specialization and just a regular overloaded function assuming you only use one or the other.
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jul 2004
Location: Somewhere in them thar hills
Posts: 23
Rep Power: 0
![]() |
I've been trying to get a better grasp on templates lately myself. From what I've read, the difference is in the utility of the template. It allows the programmer to reuse the class with any built in type or user defined class later without worrying about overloading all the functions for a specific type or group of types. This means that you can create or use a library of fairly standardized containers that will accept anything you may wish to use in them later. The details of a template must be general enough that they add the desired functionality, like a linked list or tree capability, and still don't restrict the type of object used within them. They must also grant access to any public methods that are carried within the type used by the final program. This is my half a cent worth and I hope it helps you understand the differences.
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
I understand the concept of using templates to create generic functions and see how valuable it is but when you create an explicit specialization, it takes away its "template" ability because you've now explicitly stated what the argument types are. From my point of view it is no different than just using a template named "A" and an overloaded function named "A" since the rule is to match non-template functions first, then explicit specializations and then regular template functions.
|
|
|
|
|
|
#6 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Quote:
|
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
Thanks grumpy. You confirmed what I thought.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|