每当提升的 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 函数仅调用boost::numeric::converter模板类的默认参数。其中一个参数是OverflowHandler,和用于该默认值为def_overflow_handler,但是您可以指定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.
然后指定FloatToIntRounder论点,将提供您所需的默认值,如果输入参数超出理想范围。该参数是的正常的用于从浮点型四舍五入提供一个整数,但你真的可以用它来做任何你想要的。更多信息,再加上code描述事件的顺序,在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
发布评论