Re: std::max(short,long) doesn't work
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:
>> Dear Experts,
>>
>> 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);
}
I'm not saying that anything is wrong - this just wasn't what I had
(naively) expected.
> 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.
Thanks,
Phil.
|