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

2
  • 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. Commented Oct 24, 2017 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. Commented Oct 24, 2017 at 23:09

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 - 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.
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.
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> ();

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.