lundi 25 janvier 2016

How to infer the type of the function that can return type depending on condition?

I have a template function that takes two data types - int and double and returns the one which is smaller, Now, How can I infer the type that will be returned by this function? Right now, I am losing the parts after the decimal points.

#include <iostream>

using namespace std;

template <class First, class Second>
First smaller(First a, Second b){
   return (a > b ? a : b);
}

int main () {
   int x = 100;
   double y = 15.5;
   cout<< smaller (x,y) << endl;

}

Aucun commentaire:

Enregistrer un commentaire