i have a short program, that is not compiling:
#include <algorithm>
int main(int argc, char *argv[])
{
unsigned short a = 490;
unsigned short b = 43;
unsigned short d = std::min(b, a-b);
return 0;
}
the compiler (g++ version 4.8.4) says:
./main.cpp: In function ‘int main(int, char**)’:
./main.cpp:25:47: error: no matching function for call to ‘min(short unsigned int&, int)’
unsigned short d = std::min(b, a-b);
^
./main.cpp:25:47: note: candidates are:
In file included from /usr/include/c++/4.8/algorithm:61:0,
from ./main.cpp:18:
/usr/include/c++/4.8/bits/stl_algobase.h:193:5: note: template<class _Tp> const _Tp& std::min(const _Tp&, const _Tp&)
min(const _Tp& __a, const _Tp& __b)
^
/usr/include/c++/4.8/bits/stl_algobase.h:193:5: note: template argument deduction/substitution failed:
./main.cpp:25:47: note: deduced conflicting types for parameter ‘const _Tp’ (‘short unsigned int’ and ‘int’)
unsigned short d = std::min(b, a-b);
^
In file included from /usr/include/c++/4.8/algorithm:61:0,
from ./main.cpp:18:
/usr/include/c++/4.8/bits/stl_algobase.h:239:5: note: template<class _Tp, class _Compare> const _Tp& std::min(const _Tp&, const _Tp&, _Compare)
min(const _Tp& __a, const _Tp& __b, _Compare __comp)
^
/usr/include/c++/4.8/bits/stl_algobase.h:239:5: note: template argument deduction/substitution failed:
./main.cpp:25:47: note: deduced conflicting types for parameter ‘const _Tp’ (‘short unsigned int’ and ‘int’)
unsigned short d = std::min(b, a-b);
so apparently the unsigned short subtraction results in an integer.
Question is: Why?
Edit: I saw the duplicate when searching, but didn't read to the end. They say: "Note. The minimum size of operations is int. So short/char are promoted to int before the operation is done." Which explains the behaviour..