[Libreoffice] "safe" numeric casts with explicit min/max
Lionel Elie Mamane
lionel at mamane.lu
Thu Nov 24 04:00:42 PST 2011
On Thu, Nov 24, 2011 at 11:25:30AM +0100, Lionel Elie Mamane wrote:
> On Thu, Nov 24, 2011 at 10:56:57AM +0100, Lionel Elie Mamane wrote:
>> I understand we don't want to touch that in stable branch, but anyone
>> opposing me pushing something like that in master, and similar for
>> llimit_cast and limit_cast?
>> // assumptions:
>> // \forall typename Type:
>> // ( (::std::numeric_limits< Type >::min() == 0) XOR ( ::std::numeric_limits< Type >::min() < 0 && ::std::numeric_limits< Type >::is_signed ))
>> // && (::std::numeric_limits< Type >::max() > 0 )
>> template< typename ReturnType, typename Type >
>> inline ReturnType ulimit_cast( Type nValue, ReturnType nMax )
>> (...)
>> else if ( ! ::std::numeric_limits< ReturnType >::is_bounded ||
>> ::std::numeric_limits< ReturnType >::max() > ::std::numeric_limits< Type >::max() )
> That should be:
> else if ( ! ::std::numeric_limits< ReturnType >::is_bounded ||
> nMax > ::std::numeric_limits< Type >::max() )
And even then it is buggy, because I was thinking only of the case
where nMax is ::std::numeric_limits< ReturnType >::max(), but it can
be an arbitrary (possibly negative!) value.
That thing is devilishly hard to get right... I think I have a correct
version now, but I'm going to unittest the hell out of it before I
commit, that's for sure!
--
Lionel
More information about the LibreOffice
mailing list