Fix warnings about signed/unsigned comparisons inside wxMax() and friends.

wxMax, wxMin and wxClip work correctly when called with a mix of signed and
unsigned arguments but give warnings about comparing them when compiled with
g++.

Cast both arguments to the result type, which is defined consistently with
standard C rules for implicit promotion, before comparing them to avoid this.

Also add more tests to check that using these functions in this case doesn't
provoke warnings.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65933 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2010-10-27 20:23:55 +00:00
parent f0f0542da6
commit 2a0ca9dbaf
2 changed files with 35 additions and 10 deletions

View File

@@ -63,21 +63,35 @@ template<typename T1, typename T2>
inline typename wxImplicitConversionType<T1,T2>::value
wxMax(T1 a, T2 b)
{
return (a > b) ? a : b;
typedef typename wxImplicitConversionType<T1,T2>::value ResultType;
// Cast both operands to the same type before comparing them to avoid
// warnings about signed/unsigned comparisons from some compilers:
return static_cast<ResultType>(a) > static_cast<ResultType>(b) ? a : b;
}
template<typename T1, typename T2>
inline typename wxImplicitConversionType<T1,T2>::value
wxMin(T1 a, T2 b)
{
return (a < b) ? a : b;
typedef typename wxImplicitConversionType<T1,T2>::value ResultType;
return static_cast<ResultType>(a) < static_cast<ResultType>(b) ? a : b;
}
template<typename T1, typename T2, typename T3>
inline typename wxImplicitConversionType3<T1,T2,T3>::value
wxClip(T1 a, T2 b, T3 c)
{
return (a < b) ? b : ((a > c) ? c : a);
typedef typename wxImplicitConversionType3<T1,T2,T3>::value ResultType;
if ( static_cast<ResultType>(a) < static_cast<ResultType>(b) )
return b;
if ( static_cast<ResultType>(a) > static_cast<ResultType>(c) )
return c;
return a;
}
// ----------------------------------------------------------------------------