# safe_cast<T, U>

## Synopsis

    template<class T, class U>
    T safe_cast(const U & u);

## Description

Converts one Numeric type to another. Throws an `std::out_of_range` exception if such a conversion is not possible without changing the value. This function is part of the implementation of the safe numerics library. It's been made publicly because it might be useful in related contexts.

## Type requirements

Type | Requirements
--- | ---
`T` | Numeric
`U` | Numeric

## Preconditions

The value of u must be representable by the type `T`. If this is not true, an `std::out_of_range` exception will be thrown.

## Header

`#include <boost/numeric/safe_cast.hpp>`

## Example of use

    #include <boost/numeric/safe_cast.hpp>
    #include <boost/numeric/safe_integer.hpp>

    void f(){
        safe_integer<char> i;
        unsigned char j;
        i = 1;
        j = safe_cast<unsigned char>(i);  // ok
        i = -1;
        j = safe_cast<unsigned char>(i);  // throws std::out_of_range exception
        i = 1024;
        j = safe_cast<unsigned char>(i);  // throws std::out_of_range exception
    }