View Single Post
Old Oct 6th, 2005, 10:49 PM   #1
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
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?
aznluvsmc is offline   Reply With Quote