Subject: Re: [std-proposals] add "numeric_cast" to <numeric> header
From: Qingfeng Xia (qingfeng.xia_at_[hidden])
Date: 2020-12-03 11:44:45
Update: I have completed a draft "numeric_cast" proposal for conversion
between all numeric types, with header-only C++11 implementation, and some
This header-only implementation has less feature than boost::numeric_cast
in round_policy, and handler, but it depends on C++11 compiler only. It
is ready to go without install the whole boost lib.
It supports user-defined numeric types like big integer and half floating
point, etc, if numeric_limits<> are defined.
auto b = is_numeric(a); // this is a compiling time check,
is_numeric<T>::value; // type trait
// throwable runtime numeric cast
TargetType t = numeric_cast<SourceType, TargetType>(SourceType s);
int i = func() ; short s = numeric_cast(i);
float f = func() ; short s = numeric_cast(f);
// if exception is not supported/preferred, then check if before
castbool convertible<TargetType>(SourceType sourceValue) noexcept//
can be named as `is_numeric_convertible()`
feedbacks are welcomed
Senior Research Software Engineer, UKAEA
On Sat, 16 May 2020 at 00:08, Qingfeng Xia <qingfeng.xia_at_[hidden]> wrote:
> Dear all,
> I hope runtime safe `numeric_cast` can be added to standard library header
> The boost library has already got such feature, I wonder if there is
> already a proposal to merge it into the next standard?
> Before I searched and reached Boost::numeric_cast, I reinvented the wheel,
> together with some other helper functions. More technical details are
> written in markdown at:
> I have another idea: reuse the `explicit` keyword to prevent implicit
> conversion of function parameter.
> I am not sure it is the correct maillist to discuss this idea. It proposes
> to change compiler code-base/hehaviors. Please advise, shall I send
> another email somewhere else for this idea?
> Demo code and implementation are hosted at:
> Both ideas are at the "Float the idea" stage, all the code and text are
> in early draft condition. They will be update and improve gradually.
> Thank you and looking forwards to feedback.
> Yours Sincerely,
> Qingfeng Xia
STD-PROPOSALS list run by email@example.com
Standard Proposals Archives on Google Groups