在转换为较小的数值类型之前执行范围检查的安全、跨平台方法是什么

What is a safe, cross-platform way to perform range checks before casting to a smaller numeric type?

本文关键字:安全 检查 范围 执行 是什么 方法 跨平台 转换 类型      更新时间:2023-10-16

这是我能找到的最接近的副本。

尽管有标签,但问题似乎是关于 C 的,可用的答案引用了 C99 规范。

在 C++98 中处理此检查的正确方法是什么,而无需使用 Boost 或其他库?

您可以从gsl::narrow()复制代码并稍微调整它,将其转换为返回bool can_narrow()而不是throw

// narrow_cast(): a searchable way to do narrowing casts of values
template<class T, class U>
inline constexpr T narrow_cast(U u) noexcept
{ return static_cast<T>(u); }
namespace details
{
    template<class T, class U>
    struct is_same_signedness : public std::integral_constant<bool, std::is_signed<T>::value == std::is_signed<U>::value>
    {};
}
template<class T, class U>
inline bool can_narrow(U u)
{
    T t = narrow_cast<T>(u);
    if (static_cast<U>(t) != u)
        return false;
    if (!details::is_same_signedness<T, U>::value && ((t < T{}) != (u < U{})))
        return false;
    return true;
}