pre-boost Home Libraries People FAQ More

PrevUpHomeNext

safe_unsigned_range<MIN, MAX>

Description
Template Parameters
Model of
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 any operation which would result in assigning an integer value outside of this range.

Template Parameters

Parameter Requirements Description
MIN must be a positive integer literal The minimum integer value that this type may hold
MAX must be a positive 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 with another unsigned type will result in another unsigned type fulfilling the Numeric concept. This will be the smallest unsigned integer type of sufficient size to hold the result of the operation.

Header

#include <safe/numeric/safe_range.hpp>

Example of use

#include <safe/numeric/safe_range.hpp>

void f(){
    boost::numeric::safe_unsigned_range<7, 24> i;
    i = 0;  // throws out_of_range exception
    i = 9;  // ok
    i *= 9; // throws out_of_range exception
    i = -1; // throws out_of_range exception

    std::uint8_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::uint16_t
        // j will be of type std::uint16_t 
    
}

See Also

std::out_of_range

safe_signed_range


PrevUpHomeNext