Function narrow_cast

Summary

#include <include/type_safe/narrow_cast.hpp>

(1) template <typename Target, typename Source, typename = typename std::enable_if<std::is_arithmetic<Source>::value>::type>
    constexpr TYPE_SAFE_FORCE_INLINE Target narrow_cast(const Source &source) noexcept

(2) template <typename Target, typename Source, class Policy>
    constexpr TYPE_SAFE_FORCE_INLINE auto narrow_cast(const integer< Source, Policy > &source) noexcept -> typename detail::get_target_integer< Target, Policy >::type

(3) template <typename Target, typename Source>
    constexpr TYPE_SAFE_FORCE_INLINE auto narrow_cast(const floating_point< Source > &source) noexcept -> typename detail::get_target_floating_point< Target >::type

Function overload

Synopsis

#include <include/type_safe/narrow_cast.hpp>

template <typename Target, typename Source, typename = typename std::enable_if<std::is_arithmetic<Source>::value>::type>
constexpr TYPE_SAFE_FORCE_INLINE Target narrow_cast(const Source &source) noexcept

Description

Returns
An arithmetic type with the same value as in source, but converted to to a different type. \requires The value of source must be representable by the new target type. \module types
Parameters

2 - \exclude

Mentioned in

Source

Lines 70-78 in include/type_safe/narrow_cast.hpp.

template <typename Target, typename Source,
          typename = typename std::enable_if<std::is_arithmetic<Source>::value>::type>
TYPE_SAFE_FORCE_INLINE constexpr Target narrow_cast(const Source& source) noexcept
{
    return detail::is_narrowing<Target>(source)
               ? DEBUG_UNREACHABLE(detail::precondition_error_handler{},
                                   "conversion would truncate value")
               : static_cast<Target>(source);
}

Synopsis

#include <include/type_safe/narrow_cast.hpp>

template <typename Target, typename Source, class Policy>
constexpr TYPE_SAFE_FORCE_INLINE auto narrow_cast(const integer< Source, Policy > &source) noexcept -> typename detail::get_target_integer< Target, Policy >::type

Description

Returns
A [ts::integer]() with the same value as source but of a different type. \requires The value of source must be representable by the new target type. \notes Target can either be a specialization of the integer template itself or a built-in integer type, the result will be wrapped if needed. \module types \exclude return

Mentioned in

Source

Lines 86-93 in include/type_safe/narrow_cast.hpp.

template <typename Target, typename Source, class Policy>
TYPE_SAFE_FORCE_INLINE constexpr auto narrow_cast(const integer<Source, Policy>& source) noexcept ->
    typename detail::get_target_integer<Target, Policy>::type
{
    using target_integer = typename detail::get_target_integer<Target, Policy>::type;
    using target_t       = typename target_integer::integer_type;
    return narrow_cast<target_t>(static_cast<Source>(source));
}

Synopsis

#include <include/type_safe/narrow_cast.hpp>

template <typename Target, typename Source>
constexpr TYPE_SAFE_FORCE_INLINE auto narrow_cast(const floating_point< Source > &source) noexcept -> typename detail::get_target_floating_point< Target >::type

Description

Returns
A [ts::floating_point]() with the same value as source but of a different type. \requires The value of source must be representable by the new target type. \notes Target can either be a specialization of the floating_point template itself or a built-in floating point type, the result will be wrapped if needed. \module types \exclude return

Mentioned in

Source

Lines 101-108 in include/type_safe/narrow_cast.hpp.

template <typename Target, typename Source>
TYPE_SAFE_FORCE_INLINE constexpr auto narrow_cast(const floating_point<Source>& source) noexcept ->
    typename detail::get_target_floating_point<Target>::type
{
    using target_float = typename detail::get_target_floating_point<Target>::type;
    using target_t     = typename target_float::floating_point_type;
    return narrow_cast<target_t>(static_cast<Source>(source));
}





Add Discussion as Guest

Log in