Boost logo

Boost :

Subject: Re: [boost] safe integer library -- the scope
From: Robert Ramey (ramey_at_[hidden])
Date: 2015-12-14 16:08:26


On 12/14/15 1:01 PM, Rob Stewart wrote:

> I showed a safe class template, below, that defaulted the min and max to those values, and you said that was "exactly what safe<T> is". If yours has those defaults, then there is no such tedium.
>
>>>>>> However, since min() and max() are now constexpr, that all can be
>>>>>> collapsed into a single template with three parameterizing types:
>>>> the
>>>>>> underlying type, the minimum, and the maximum:
>>>>>>
>>>>>> template
>>>>>> <
>>>>>> class T
>>>>>> , T Min = std::numeric_limits<T>::min()
>>>>>> , T Max = std::numeric_limits<T>::max()
>>>>>>>
>>>>>> class safe;
>>>>
>>>> That's exactly what safe<T> is.
>>>
>>> Why doesn't that suffice for all use cases?

Whoops, I read the above too fast.

The reason is the we need to be able to specify policy classes for
exception handling and type promotion. The actual template is

template<
     class Stored,
     Stored Min,
     Stored Max,
     class P, // promotion polic
     class E // exception policy
>
safe_base;

then we have

template <
     class T,
     class P = native,
     class E = throw_exception
>
using safe = safe_base<
     T,
     std::numeric_limits<T>::min(),
     std::numeric_limits<T>::max(),
     P,
     E
>;

and

template <
     std::intmax_t MIN,
     std::intmax_t MAX,
     class P = native,
     class E = throw_exception
>
using safe_signed_range = safe_base<
     typename detail::signed_stored_type<MIN, MAX>,
     static_cast<typename detail::signed_stored_type<MIN, MAX> >(MIN),
     static_cast<typename detail::signed_stored_type<MIN, MAX> >(MAX),
     P,
     E
>;

So it's really just a question of getting the default parameters correct
for different usages.

Robert Ramey


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk