A
Asif Zaidi
Hi:
Learning templates now...
I can code a template for min(1,2) and min(1.0, 2.3) using the same
class definition.
I am trying to do min(1, 0.8) and this is where I am stuck.
I am not getting the definition right for my_min function - I tried
googling on various variations of templates but I probably don't know
the right term to google for.
My code is as follows. My reasoning is, since my_min will have 2
different type arguments, I need (T, U) and my return type is U (which
is 2nd arg).
I am getting compile errors (See ERROR below). Any help is
appreciated.
==============================================
#include <iostream>
using namespace std;
template <class T, class U>
struct MinPromotion;
template <>
struct MinPromotion<int, double>
{
typedef double PromotedType;
};
// ERROR
template<class T, class U>
const U & <class MinPromotion<T, U>:romotedType> my_min (const T
&x, const U &y)
{
return x < y ? x : y;
}
template<class T>
const T &my_min(const T &x, const T &y)
{ return x < y ? x : y; }
int main()
{
cout << my_min(1,2) << endl;
cout << my_min(2.2, 3.2) << endl;
// cout << my_min(1, 0.7) << endl;
}
=================
Learning templates now...
I can code a template for min(1,2) and min(1.0, 2.3) using the same
class definition.
I am trying to do min(1, 0.8) and this is where I am stuck.
I am not getting the definition right for my_min function - I tried
googling on various variations of templates but I probably don't know
the right term to google for.
My code is as follows. My reasoning is, since my_min will have 2
different type arguments, I need (T, U) and my return type is U (which
is 2nd arg).
I am getting compile errors (See ERROR below). Any help is
appreciated.
==============================================
#include <iostream>
using namespace std;
template <class T, class U>
struct MinPromotion;
template <>
struct MinPromotion<int, double>
{
typedef double PromotedType;
};
// ERROR
template<class T, class U>
const U & <class MinPromotion<T, U>:romotedType> my_min (const T
&x, const U &y)
{
return x < y ? x : y;
}
template<class T>
const T &my_min(const T &x, const T &y)
{ return x < y ? x : y; }
int main()
{
cout << my_min(1,2) << endl;
cout << my_min(2.2, 3.2) << endl;
// cout << my_min(1, 0.7) << endl;
}
=================