我有一个简短的程序,不编译:
#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;
}
编译器(g ++版本4.8.4)说:
./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);
所以显然无符号短减法产生一个整数。
问题是:为什么?
编辑: 我在搜索时看到了副本,但没有读到最后。他们说: 的&#34;注意。最小操作大小为int。因此在操作完成之前将short / char提升为int。&#34;这解释了行为......