升压numeric_cast<>具有默认值,而不是一个异常? [英] Boost numeric_cast<> with a default value instead of an exception?

查看:98
本文介绍了升压numeric_cast<>具有默认值,而不是一个异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当提升的 numeric_cast<> 转换失败,它抛出一个异常。是否有提振了类似的模板,让我指定一​​个默认值,而不是,或捕捉异常,我可以在这种情况下,唯一能做的?

Whenever boost's numeric_cast<> conversion fails, it throws an exception. Is there a similar template in boost that lets me specify a default value instead, or is catching the exception the only thing I can do in this case?

我不是太担心所有额外的异常处理的表现,但我宁愿使用一个标准的模板比写无用的包装功能。此外,从过去的经验,我认为这很可能是提振居然有我在想什么,我只是还没有发现它。

I'm not too worried about the performance of all the extra exception handling, but I'd rather use a standard template than write useless wrapper functions. Besides, from past experience, I thought it's likely that boost actually has what I'm thinking of, and I simply haven't found it.

推荐答案

numeric_cast 函数仅调用<一个href=\"http://www.boost.org/doc/libs/1_34_0/libs/numeric/conversion/doc/converter.html\"><$c$c>boost::numeric::converter模板类的默认参数。其中一个参数是<一个href=\"http://www.boost.org/doc/libs/1_34_0/libs/numeric/conversion/doc/converter_policies.html#oh\"><$c$c>OverflowHandler,和用于该默认值为<一个href=\"http://www.boost.org/doc/libs/1_34_0/libs/numeric/conversion/doc/converter_policies.html#oh_def\"><$c$c>def_overflow_handler,但是您可以指定<一个href=\"http://www.boost.org/doc/libs/1_34_0/libs/numeric/conversion/doc/converter_policies.html#oh_silent\"><$c$c>silent_overflow_handler共进晚餐preSS例外吧。

The numeric_cast function simply calls the boost::numeric::converter template class with the default arguments. One of the arguments is OverflowHandler, and the default value for that is def_overflow_handler, but you can specify silent_overflow_handler to suppress the exception instead.

然后指定<一个href=\"http://www.boost.org/doc/libs/1_34_0/libs/numeric/conversion/doc/converter_policies.html#f2i\"><$c$c>FloatToIntRounder论点,将提供您所需的默认值,如果输入参数超出理想范围。该参数是的正常的用于从浮点型四舍五入提供一个整数,但你真的可以用它来做任何你想要的。更多信息,再加上code描述事件的顺序,在<一个href=\"http://www.boost.org/doc/libs/1_34_0/libs/numeric/conversion/doc/converter.html\"><$c$c>converter文档。

Then specify the FloatToIntRounder argument that will provide your desired default value if the input argument is outside your desired range. The argument is normally used for providing an integer for rounding from a floating-point type, but you can really use it for whatever you want. More information, plus code describing the sequence of events, at the converter documentation.

据我所知,升压没有你在想什么,但它提供了便利,为您建立它自己。

As far as I know, Boost doesn't have what you're thinking of, but it provides the facility for you to build it yourself.

这篇关于升压numeric_cast&LT;&GT;具有默认值,而不是一个异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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