c++ - c++ 有等效的 boost::numeric_cast<DestType>(SourceType) 吗?

标签 c++ templates boost floating-point integer-overflow

我正在做一堆应用数学/信号处理/算法 C++ 代码。

我已经启用了 -Wconversion编译器警告捕获类型为 double 的数字的运行时转换等问题输入 int32_t .

显然,在这些转换过程中我总是很担心,因为:

  • 数字十进制值丢失
  • 可能的正溢出或负溢出(“正溢出”是指 double 值大于 INT32_MAX 并尝试将该值存储在目标类型中(在本例中为 int32_t))

每当我担心这种转换时,我通常会使用单行检查:

boost::numeric_cast<DestType>(SourceType)

但是我想在没有 boost 的情况下做同样的事情.

直接 C++ 是否有等效的 boost::numeric_cast<DestType>(SourceType) ?

如果直接的 C++ 没有等效项,那么什么是可比较的非 boost实现?

我认为有点类似的检查基本上是一个模板函数,它有一个 if 语句来检查输入参数是否正溢出或负溢出(通过使用 std::numeric_limits<DestType> ::max()::min() 并抛出异常).

最佳答案

As @SergeyA stated , 没有标准的 C++ 规范目前没有相当于 Boost 的 boost::numeric_cast<typename Destination>(Source value) .

这是一个仅使用标准 C++ 的直接实现:

template<typename Dst, typename Src>
inline Dst numeric_cast(Src value)
{
    typedef std::numeric_limits<Dst> DstLim;
    typedef std::numeric_limits<Src> SrcLim;

    const bool positive_overflow_possible = DstLim::max() < SrcLim::max();
    const bool negative_overflow_possible =
            SrcLim::is_signed
            or
            (DstLim::lowest() > SrcLim::lowest());

    // unsigned <-- unsigned
    if((not DstLim::is_signed) and (not SrcLim::is_signed)) {
        if(positive_overflow_possible and (value > DstLim::max())) {
            throw std::overflow_error(__PRETTY_FUNCTION__ +
                                      std::string(": positive overflow"));
        }
    }
    // unsigned <-- signed
    else if((not DstLim::is_signed) and SrcLim::is_signed) {
        if(positive_overflow_possible and (value > DstLim::max())) {
            throw std::overflow_error(__PRETTY_FUNCTION__ +
                                      std::string(": positive overflow"));
        }
        else if(negative_overflow_possible and (value < 0)) {
            throw std::overflow_error(__PRETTY_FUNCTION__ +
                                      std::string(": negative overflow"));
        }

    }
    // signed <-- unsigned
    else if(DstLim::is_signed and (not SrcLim::is_signed)) {
        if(positive_overflow_possible and (value > DstLim::max())) {
            throw std::overflow_error(__PRETTY_FUNCTION__ +
                                      std::string(": positive overflow"));
        }
    }
    // signed <-- signed
    else if(DstLim::is_signed and SrcLim::is_signed) {
        if(positive_overflow_possible and (value > DstLim::max())) {
            throw std::overflow_error(__PRETTY_FUNCTION__ +
                                      std::string(": positive overflow"));
        } else if(negative_overflow_possible and (value < DstLim::lowest())) {
            throw std::overflow_error(__PRETTY_FUNCTION__ +
                                      std::string(": negative overflow"));
        }
    }

    // limits have been checked, therefore safe to cast
    return static_cast<Dst>(value);
} 

注意事项:

  • 编译器是 g++ 版本 4.8.5。
  • 编译器标志:
    • -std=c++0x
    • -O0
    • -g3
    • -迂腐
    • -迂腐的错误
    • -墙
    • -Wextra
    • -错误
    • -W转换
    • -c
    • -fmessage-length=0
    • -W符号转换
    • -fPIC
    • -MMD
    • -国 session 员
  • 对于浮点类型你不能使用std::numeric_limits<float>::min()但必须使用 std::numeric_limits<Dst>::lowest()因为::min返回的是 1e-38 而不是负 float
  • 很多 std::numeric_limits是 const 表达式,因此编译器将能够在编译时极大地简化它(即 N 个 if 语句将在编译时减少为一个 if 语句或没有)

关于c++ - c++ 有等效的 boost::numeric_cast<DestType>(SourceType) 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49658182/

相关文章:

c++ - 传递一个变量来加载位图文件?

C++ 在运行时获取带有 ID 的对象

c++ - 展平 n 维 vector

c++ - 模板函数的动态调度?

c++ - 将世界、 View 和投影矩阵传递给 Direct3D 中的着色器

c++ - 预处理器和模板参数或代码段的条件编译

c++ - boost::read_graphviz - 如何读出属性?

c++ - 我如何从没有复制构造函数的函数返回一个类?

c++ - 从 dock 启动进程与 OS X 上的命令行有什么区别

c++ - 如何迭代两个类似 STL 的容器(笛卡尔积)