Archived

This topic is now archived and is closed to further replies.

hapaboy55

Template Specialization Problem

Recommended Posts

Hi all I''m having a problem with some template specialization code I''ve written. Could someone please explain why this will compile: template T maxn(T arr[], int size){ return arr[0];}; template<> const char * maxn(const char * arr[], int size){ return arr[0];}; And this will not: template const T maxn(const T arr[], int size){ return arr[0];}; template<> char * maxn(char * arr[], int size){ return arr[0];}; From experimentation, it seems if I add the const modifier to the template, the template specialization will no longer match it no matter what I do with it. The error given is: error C2912: explicit specialization; ''char *maxn(char *[],int)'' is not a specialization of a function template I would appreciate any help with this =)

Share this post


Link to post
Share on other sites
if you expand the template


template
const T maxn(T arr[], int size) { return arr[0]; }


with T= char*


template<> const char* maxn( char* arr[], int size) { return arr[0]); }


the const-modifier is still there. so you can't specialize the template without the "const".

[edited by - sphinx23 on November 15, 2003 10:12:20 AM]

Share this post


Link to post
Share on other sites
Thanks for your help Sphinx but I still can''t get it to work. This combination will not work either for me:

template
const T maxn(const T arr[], int size){ return arr[0];};
template<>
const char * maxn(const char * arr[], int size){ return arr[0];};


In fact, no combination of adding/removing const modifiers from the specialization seems to work unless I remove the const modifiers from the template.

Share this post


Link to post
Share on other sites
Ok I finally figured it out. For those who may be curious, it turns out that for this declaration:

template
const T maxn(const T arr[], int size){ return arr[0];};

Using the const modifier for a type T means that that type will be constant...no surprise there...the problem happens if you want to do a specialization which uses a pointer. The above would make the pointer constant, not what its pointing to.

Following my example. Using char * as the type for the specialization will translate to "a const pointer to a char array", it isn''t just substituting the T with char *.

so the correct specialization of the above to have a pointer to a const char * array would be this:
template<>
const char * const maxn(const char * const arr[], int size){ return arr[0];};

Now that brings up a new question...how do you create a template with pointers to constant types without using specializations?

Share this post


Link to post
Share on other sites
quote:
Original post by hapaboy55

Now that brings up a new question...how do you create a template with pointers to constant types without using specializations?


Are you looking for something like this?


template <class T>
const T* maxn(const T* arr[], int size){ return arr[0];}


Or, am I misunderstanding?

edit: put in source tags so code would display correctly

[edited by - CrazyMike on November 15, 2003 7:03:39 PM]

Share this post


Link to post
Share on other sites