Template Question

Trying to create a template that will create a 'new' object and return either a reference to the object or an integer. This is what i'm trying to do:

1
2
3
4
5
template <class ClassType, ReturnType> ReturnType CreateObject()        
{
        if (ReturnType == SomeType)      return new ClassType;
   else if (ReturnType == SomeOtherType) return someInt;
}



I'm just unsure if i can ever do a conditional statement like that.
You didn't specify if ReturnType was a class/typename or an existing type.
And no, you can't really do that at all...but,you can specialize templates:
1
2
3
4
5
6
7
8
9
10
template<typename T>
T CreateObject()
{
  return(new T);
}
template<> //this will be a template specialization that is
int CreateObject<int>() //specialized for an int
{
  return(1337);
}


Thus, CreateObject<string>() returns a new string, whereas CreateObject<int>() returns 1337.
Yup that's exactly what I'm looking for. Thanks LB
Topic archived. No new replies allowed.