Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 6th, 2005, 9: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
Old Oct 7th, 2005, 2:00 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
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
}
with this;
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
}
and this;
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
}
grumpy is offline   Reply With Quote
Old Oct 7th, 2005, 9:19 AM   #3
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
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.
aznluvsmc is offline   Reply With Quote
Old Oct 7th, 2005, 9:37 AM   #4
Griz803
Newbie
 
Join Date: Jul 2004
Location: Somewhere in them thar hills
Posts: 23
Rep Power: 0 Griz803 is on a distinguished road
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.
Griz803 is offline   Reply With Quote
Old Oct 7th, 2005, 9:47 AM   #5
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
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.
aznluvsmc is offline   Reply With Quote
Old Oct 7th, 2005, 10:17 AM   #6
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by aznluvsmc
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.
Not a lot. Function templates are just a means of creating multiple overloaded functions. The issues come in when you use them, when there is a function template and a function of the same name which is not a template function. Esssentially, if the arguments supplied to the function are of the same type as the non-template form (or can be trivially converted to those types), the non-template function will be called. If it can't, then the compiler attempts to call an instantiation of the template function. If an explicit instantiation exists (i.e. template <> ...) that that explicit instance acts just like any other overloaded function (which means it will be called just like a non-template function). Otherwise, the compiler attempts to identify an implicit instantiation (which means it will instantiate the template in a way that means EXACT matching of argument types) and call that. If that step fails, an error has occurred, as there is no function to call.
grumpy is offline   Reply With Quote
Old Oct 7th, 2005, 11:37 PM   #7
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
Thanks grumpy. You confirmed what I thought.
aznluvsmc is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:25 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC