Casts one type of number to another type.

template<typename T, typename U>
inline bool SafeCast (
   const T From,
   U& To


  • [in] From
    The source number to convert. This must be of type T.

  • [out] To
    A reference to the new number type. This must be of type U.

Return Value

true if no error occurs; false if an error occurs.


This method is part of SafeInt Library and is designed for a single casting operation without creating an instance of the SafeInt Class.


This method should only be used when a single operation must be protected. If there are multiple operations, you should use the SafeInt class instead of calling the individual stand-alone functions.

For more information about the template types T and U, see SafeInt Functions.


Header: safeint.h

Namespace: Microsoft::Utilities

See Also


SafeInt Class

Other Resources

SafeInt Functions

SafeInt Library