P
Phil Endecott
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:
#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.
Presumably this is a consequence of these functions returning
references, not values; you can't return a reference to something that
could be a short or could be a long, and promoting to the larger size
and returning a reference to the temporary would have its own issues.
Any thoughts? Would it be possible to write further overloads for min
and max functions that match different argument types return values not
references?
Regards,
Phil.
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:
#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.
Presumably this is a consequence of these functions returning
references, not values; you can't return a reference to something that
could be a short or could be a long, and promoting to the larger size
and returning a reference to the temporary would have its own issues.
Any thoughts? Would it be possible to write further overloads for min
and max functions that match different argument types return values not
references?
Regards,
Phil.