C++ Function Template With Flexible Return Type

Posted by Ignatius Reza on Stack Overflow See other posts from Stack Overflow or by Ignatius Reza
Published on 2010-12-25T16:19:08Z Indexed on 2010/12/25 16:54 UTC
Read the original article Hit count: 209

Filed under:
|

Let's say that we have a function like so

template <class T, class T2>
T getMin(T a, T2 b) {
  if(a < b)
    return a;
  return b;
}

if we call the function like so

int a, b;
long c;

a = getMin(b, c);

if c is < a, then the value of c will be type casted to int.

Is it possible to make the return type flexible so that it would return an int, or long, or any other type considered smaller by "<" without being type casted?

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates