template specialization question

Hi, could you please tell me what I am doing wrong with this specialization that is would create and return a newly allocated char*?
1
2
3
4
5
6
7
8
9
10
template<typename T>
inline T* CreateUnit(T t)
{
    T* temp = new T;
    *temp = t;
    return temp;
}

template <>
char* CreateUnit <char*>(int length);


The first template seems to work just fine.
The second one gives me this error no matter how I try to modify the syntax.
"|19|error: template-id 'CreateUnit<char*>' for 'char* CreateUnit(int)' does not match any template declaration|"

Removing the argument does nothing, changing the "<char*>" to "<char>" does nothing.
Googling has not helped. Infact, most seem to propose the same thing I have here, but it won't compile.

Any help would be great, thanks.
The parameter is also templatized on line 2 and must be char, and you want <char> instead of <char*>.
The template declaration says that the function takes a T and returns a T*.

The attempted specialisation is wrong because none of the actual types ypu specify match.

You are returning char* but the function parameter is int. and the type specified
in the <> brackets is char*

What do you want from the specialization - then maybe we can show you how to do it.


EDIT:
Re-reading your original post you say specialization that is would create and return a newly allocated char*?
So the specialization should read:
1
2
template <>
char* CreateUnit <char>(char);


or
char* CreateUnit (char); //let compiler deduce T based on parameter type


**note - the function code can be reduced to a one-liner
return new T(t);
Last edited on
I simply want the specialization to create a newly allocated NBTS style string.
1
2
char* temp = new char[length];
return temp;


A simple string generator that is to be passed to a resource manager.

How on earth would I specialize this?
1
2
template <>
char* CreateUnit <char*>(int length);

It needs to take an int for the length, so I'm not sure how to approach this as a specialization.
And please don't insist I just don't use specialization, I'd really like to know how to do it.

I would like to be able to explicitly instantiate them like I can the generic template, ex
1
2
3
CreateUnit<int>();
CreateUnit<double>();
CreateUnit<char>();

ect.

Any suggestions?
You can't make a specialization that takes different parameters. That's called an overload.
I don't think it's clear what you are trying to do. Are you trying to make a function that creates some number of units?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
template<typename T>
T *CreateUnits(int num)
{
    return new T[num];
}

//Which you could specialize:

template<>
char *CreateUnits<char *>(int num)
{
    // This isn't really a specialization - perhaps you have some special code to go here.
    return new char[num];
}
Refer to firedraco 's answer.
Yeah thats what I was kind of figuring, I'd have to ditch the specialization for simple overloading. Oh well, thanks for your help.
Topic archived. No new replies allowed.