Templates question

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>::promotedType> 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;
}

=================
 
V

Vladimir Jovic

Asif said:
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>::promotedType> my_min (const T
&x, const U &y)
{
return x < y ? x : y;
}

What you wrote there makes no sense. This:
template<class T, class U>
const typename MinPromotion< T, U >::promotedType& my_min (const T &x,
const U &y)
{
return x < y ? x : y;
}
compiles, and it looks like it satisfies your requirements, but it is an
undefined behaviour to return a reference to different types. For that
reason, this would be ok:
template<class T, class U>
const typename MinPromotion< T, U >::promotedType my_min (const T &x,
const U &y)
{
return x < y ? x : y;
}

BTW why don't you use std::min?
 
C

Clement Cousin

Be careful with this solution. It assumes that you compare types for
which the operator< is defined.
You should perform a static check before performing the comparison.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top