0

I have a static non-member function which returns a template class object depending on the type of the object.

template< typename T >
class Example
{
.... 
};

static Example non_member_function(int var) {
  if (var == 1)
     return Example<float>;
  else
     return Example<int>
}

This is not working as return type Example is a template. How can I write this return type

user3566905
  • 103
  • 1
  • 5
  • 1
    You can't return an unspecialized `Example`. I don't think you'll be any better off with `std::variant` in this case. I think this calls more for inheritance or a design rethink. More context please. – user4581301 Oct 24 '17 at 23:05
  • 2
    This is likely an X/Y Problem. More details about why you wanted this to work might help someone suggest a better alternative. – aschepler Oct 24 '17 at 23:09

3 Answers3

1

You cannot use different types in the return value without making the function a template too - each return type defines a new function, they are all different.

The better approach is to return a pointer, which does allow polymorphism.

Note though that you are then returning a pointer to a local object, which is undefined after the function ends. You would need to return a new object (return new Example<float>;), or make the two objects static inside the function and return their addresses -depending if you want to return each time a new object, or always the same one.

Aganju
  • 5,994
  • 1
  • 9
  • 22
  • 1
    Returning by value does a copy or move of a local object when necessary (and can often avoid doing either). There's no need to jump to returning pointers or references, and definitely no need to resort to a raw `new`. – aschepler Oct 24 '17 at 23:07
  • @aschepler - did you read his question? He _needs_ to move to pointers as it is not possible to return references to different types. The remark applies to the consequences of doing it. – Aganju Oct 24 '17 at 23:09
  • Appropriate alternatives might involve polymorphism, refactoring into multiple functions, templates, `variant`, `any`, callable objects, or other approaches. I don't see enough information in the question to recommend a particular starting point. – aschepler Oct 24 '17 at 23:22
0

It doesn't really work like that - the compiler needs to know what the return type is, and you're returning either Example <int> or Example <float> (which are different types) depending on a variable passed in at runtime.

If you know what type you want at compile time, you can do this:

template <typename T> static Example<T> non_member_function() {
     return Example<T> ();
}

And then call it like this:

Example <int> example1 = non_member_function <int> ();

or

Example <float> example2 = non_member_function <float> ();
Steve
  • 1,716
  • 8
  • 18
0

C++ does not (directly) allow what you're trying to do. Example<float> and Example<int> are unrelated classes that do not necessarily have anything at all in common. So you can't have a single function return two different things, any more than you could write a function that sometimes returns a double and sometimes returns a std::vector<int>.

There are some ways to do similar things, but which is appropriate really depends on why you want the function to act this way and what you intend to do with the returned values.

aschepler
  • 65,919
  • 8
  • 93
  • 144