SafeNumeric<T>DescriptionThis holds an arithmetic value which can be used as a replacement
for built-in C++ arithmetic values. These types differ from their built-in
counter parts in that the are guaranteed not to produce invalid arithmetic
results. These operations return safe types rather than built-in
types.Refinement ofNumeric or IntegerNotationSymbolDescriptionT, UTypes fulfilling Numeric or Integer type
requirements.t, uobjects of types T, USA type fulfilling SafeNumeric type requirementss, s1, s2objects of types SopC++ infix operator supported by underlying type Tprefix_opC++ prefix operator: -, +, ~, ++, -- supported by
underlying type Tpostfix_opC++ postfix operator:++, -- supported by underlying type
Tassign_opC++ assignment operatorValid ExpressionsExpressionResult TypeDescriptions op tunspecified Sinvoke C++ operator op and return another
SafeNumeric type.t op sunspecified Sinvoke C++ operator op and return another
SafeNumeric type.s1 op s2unspecified Sinvoke C++ operator op and return another
SafeNumeric type.prefix_op Sunspecified Sinvoke C++ operator prefix_op and
return another SafeNumeric type.S postfix_opunspecified Sinvoke C++ operator postfix_op and
return another SafeNumeric type.s assign_op tS &convert t to type S and assign it to s.
t assign_op sT &convert s to type T and assign it to s. If the
value t cannot be represented as an instance of type S, it is an
error.S(t)Sconstruct an instance of S from a value of type T.
In this case, T is referred to as the base type of S. If the
value t cannot be represented as an instance of type S, it is an
exception condition is invoked. SSconstruct an uninitialized instance of
S.T(s)Timplicit conversion of the value of s to type T. If
the value of s cannot be correctly represented as a type T, an
exception condition is invoked.static_cast<T>(s)Tconvert the value of s to type T. If the value of s
cannot be correctly represented as a type T, an exception
condition is invoked. is_safe<S>std::true_typetype trait to query whether any type S fulfills the
requirements for a SafeNumeric type.base_type<S>::typeTRetrieve the base type of a given safe
type.base_value(s)TRetrieve the value of an instance of a safe type.
This is equivalent to
static_cast<base_type<S>>(s).The result of any binary operation where one or both of the
operands is a SafeNumeric type is also a SafeNumeric type.All the expressions in the above table are
constexpr expressions.Binary expressions which are not assignments and whose operands
are both safe types require that promotion and exception policies of
the operands be identical.Operations on safe types are supported if and only if the
same operation is supported on the underlying types. For example, the
binary operations |, &, ^ and
~ operations defined for safe unsigned integer types. But
they are not defined for floating point types. Currently the are also
defined for signed integer types. It's not clear that this is the
correct decision. On one hand, usage of these operators on signed
types is almost certainly an error in program logic. But trapping this
as an error conflicts with the goal of making safe types "drop-in"
replacements for the corresponding built-in types. In light of this,
these operators are currently supported as they are for normal
built-in types.Safe Numeric types will be implicitly converted to built-in
types when appropriate. Here's an example:void f(int);
int main(){
long x;
f(x); // OK - builtin implicit version
safe<long> y;
f(y);
return 0;
}This behavior supports the concept of
safe<T> as being a "drop-in" replacement for a
T.InvariantsThe fundamental requirement of a SafeNumeric type is that it
implements all C++ operations permitted on its base type in a way the
prevents the return of an incorrect arithmetic result. Various
implementations of this concept may handle circumstances which produce
such results differently (throw exception, compile time trap, etc..). But
no implementation should return an arithmetically incorrect result.Modelssafe<T>safe_signed_range<-11, 11>safe_unsigned_range<0, 11>safe_signed_literal<4>Header#include
<boost/numeric/safe_numerics/concepts/safe_numeric.hpp>