pre-boost Home Libraries People FAQ More

PrevUpHomeNext

safe_signed_range<boost::intmax_t MIN, boost::intmax_t MAX>

Description
Notation
Template Parameters
Model of
Valid Expressions
Header
Example of use
See Also

Description

This type holds a integer in the range [MIN, MAX]. It will throw a std::out_of_range exception for operation which would result in assigning an integer value outside of this range.

Notation

Symbol Description
T, U, V Types which model the Numeric concept
t, u, v objects of types T
ssr object of type safe_signed_range
ssr(t) some safe range with value t
S some safe type

Template Parameters

Parameter Requirements Description
MIN must be an integer literal The minimum integer value that this type may hold
MAX must be an integer literal The maximum integer value that this type may hold
MIN < MAX  

Model of

Numeric

The usage of this type in an arithmetic expression will result in another type fulfilling the Numeric concept.

Operations on safe_signed_range will result in the same

Valid Expressions

Expression Result Type Description
ssr op u S(ssr(t op u))

op is any valid binary operator for type T

t op su S(ssr(t op u))

op is any valid binary operator for type T

ssr op su S(ssr(t op u))

op is any valid binary operator for type T

safe_signed_range(MIN, MAX) safe<T>

construct a instance of safe<T> from t

Header

#include <boost/safe_numerics/safe_range.hpp>

Example of use

#include <safe/numeric/safe_range.hpp>

void f(){
    boost::numeric::safe_signed_range<7, 24> i;
    i = 0; // error
    i = 9; // ok
    i *= 9; // throws overflow exception

    std::int8_t j = 4;
    auto k = i + j;
        // since i can vary between 7 and 24 and j can vary between 0 and 255
        // the smallest unsigned integer which can hold the result std::int16_t
        // j will be of type std::int16_t 
}

See Also

std::out_of_range

safe_unsigned_range


PrevUpHomeNext