Re: std::max(short,long) doesn't work
On Oct 18, 6:34 pm, Phil Endecott <spam_from_usenet_0...@chezphil.org>
wrote:
Hi Neelesh, thanks for the quick reply.
Neelesh Bodas wrote:
On Oct 18, 9:19 pm, Phil Endecott <spam_from_usenet_0...@chezphil.org>
wrote:
I'm surprised to find that std::max doesn't work (i.e. won't compile) =
if
the arguments are not of exactly the same type, e.g. one is a short and
the other is a long:
thats because the template is defined to take two arguments of same
type
Indeed, but I'm surpised that the short isn't promoted to a
long as it would be for a non-template function where both
arguments have the same type:
int f(long x, long y) {
return 1;
}
int g() {
short s;
long l;
return f(s,l);
}
And how should the compiler know that it isn't the opposite
which is wanted. In the above case, you've specified exactly
that there is a single f. With the template, the compiler
could, potentially, instantiate either max(long,long) or
max(short,short). (If you'd written f(short, short) above, it
would also have worked.)
I'm not saying that anything is wrong - this just wasn't what
I had (naively) expected.
Templates rarely do:-).
Yes you can, just explicitly provide template arguments:
std::max<long>(s,l);
Ah, that's interesting. So if I provide an explicit type then it
behaves like my non-template function f above.
If you provide a specific type, the compiler doesn't have to
deduce it. The problem only occurs because the compiler can't
deduce the type when the two arguments have different types.
There have been many different attempts to provide a max/min
which actually work as expected, but it's far from trivial.
Especially as expectations vary:-). For what you want, you'd
need something like:
template< typename T1, typename T2 >
typename ReturnType< T1, T2 >::type
max( T1 x, T2 y )
{
return x > y ? x : y ;
}
Where ReturnType is something like:
template< typename T1, typename T2 > class ReturnType ;
template< typename T >
class ReturnType< T, T >
{
public:
typedef T type ;
} ;
// Lot's of specializations for mixed types...
Others want max to use only references, so you can write
something like:
max(a,b) = 0 ;
This requires something like:
template< typename T >
T& max( T& a, T& b );
I'm not sure what overload resolution would do if you have both
(although a few simple examples seem to work with g++).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34