c ++是否具有等效的boost :: numeric_cast< DestType>(SourceType)? [英] does c++ have an equivalent boost::numeric_cast<DestType>(SourceType)?

查看:83
本文介绍了c ++是否具有等效的boost :: numeric_cast< DestType>(SourceType)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am doing a bunch of applied-mathematics/signal-processing/algorithms C++ code.

我已启用-Wconversion编译器警告来捕获诸如double类型到int32_t类型的数字的运行时转换之类的问题.

I have enabled the -Wconversion compiler warning to catch issues like runtime conversion of numbers that are type double to type int32_t.

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

Obviously I am always concerned during these conversions because:

  • 数字十进制值的损失
  • 可能的正向或负向溢出(正向溢出"是当double的值大于INT32_MAX并尝试将该值存储为目标类型(在这种情况下为int32_t))

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

Whenever I am concerned about such a conversion I usually use the one-liner check:

boost::numeric_cast<DestType>(SourceType)

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

However I would like to do the same thing without boost.

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

Does straight C++ have an equivalent boost::numeric_cast<DestType>(SourceType)?

如果纯正的C ++没有等效的版本,那么可比的非boost的实现又是什么呢?

If straight C++ does not have an equivalent, what would be a comparable non-boost implementation?

我认为某种程度可比的检查基本上是一个模板函数,该函数具有单个if语句,以检查输入参数是否出现正向或负向溢出(通过使用std::numeric_limits<DestType> ::max()::min()并引发异常) ).

I would think a somewhat comparable check would be basically a template function that has a single if statement to check the input parameter against positive or negative overflow (by using the std::numeric_limits<DestType> ::max() and ::min() and throws an exception).

推荐答案

As @SergeyA stated, no the standard C++ spec does not currently have an equivalent to Boost's boost::numeric_cast<typename Destination>(Source value).

这是仅使用标准C ++的简单实现:

Here is a straight-forward implementation that uses only standard 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
    • 学究的
    • -pedantic-errors
    • -墙
    • -Wextra
    • -恐怖
    • -Wconversion
    • -c
    • -fmessage-length = 0
    • -Wsign转换
    • -fPIC
    • -MMD
    • -MP
    • compiler is g++ version 4.8.5.
    • compiler flags:
      • -std=c++0x
      • -O0
      • -g3
      • -pedantic
      • -pedantic-errors
      • -Wall
      • -Wextra
      • -Werror
      • -Wconversion
      • -c
      • -fmessage-length=0
      • -Wsign-conversion
      • -fPIC
      • -MMD
      • -MP

      这篇关于c ++是否具有等效的boost :: numeric_cast&lt; DestType&gt;(SourceType)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆