Re: std::max(short,long) doesn't work
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
> #include <algorithm>
>
> int f(short s, long l) {
> return std::max(s,l);
>
> }
>
> $ g++ -W -Wall -c /tmp/maxtest.cc
> /tmp/maxtest.cc: In function 'int f(short int, long int)':
> /tmp/maxtest.cc:5: error: no matching function for call to 'max(short
> int&, long int&)'
>
> I can't even compare a long with an integer constant.
Yes you can, just explicitly provide template arguments:
std::max<long>(s,l);
Alternatively, do an explicit cast : std::max((long)s,l);
-Neelesh
|