cnl
cnl copied to clipboard
Way of converting between types of different overflow tags
What is your question?
Is there a way of converting between datatypes with different overflow tags.
What are you trying to do?
One may need to change the overflow type within an algorithm.
TEST(math, convert_overflow_type)
{
auto a = cnl::static_number<16,-15, cnl::neg_inf_rounding_tag, cnl::native_overflow_tag, int16_t>{0.5};
auto b = cnl::static_number<16,-15, cnl::neg_inf_rounding_tag, cnl::native_overflow_tag, int16_t>{0.5};
auto c = a + b;
cnl::static_number<16,-15, cnl::neg_inf_rounding_tag, cnl::saturated_overflow_tag, int16_t> d = c;
std::cout << "d = " << d << std::endl;
}
Going through rep works but is quite cumbersome:
static constexpr auto a = cnl::static_number<16,-15, cnl::neg_inf_rounding_tag, cnl::native_overflow_tag, int16_t>{0.5};
static constexpr cnl::static_number<16,-15, cnl::neg_inf_rounding_tag, cnl::saturated_overflow_tag, int16_t> b = cnl::from_rep<cnl::static_number<16,-15, cnl::neg_inf_rounding_tag, cnl::saturated_overflow_tag, int16_t>, int16_t> {}(static_cast<int16_t>(to_rep(a)));
static constexpr auto expected = cnl::static_number<16,-15, cnl::neg_inf_rounding_tag, cnl::saturated_overflow_tag, int16_t>{0.5};
static_assert(identical(expected, b));
There's no such API currently.
I took a look at this yesterday. There is a set_rounding_t
alias which does a near-identical thing for rounding_integer
. But a quick fix to copy/paste this as set_overflow_t
isn't as quite simple as it sounds and merely highlights the fact that there is a more general-purpose solution fighting to get out.
I think that adapting set_tag_t
so that it works recursively, is probably the way to go. This will replace set_rounding_t
and be used thus:
constexpr auto b{set_tag_t<decltype(a), saturated_overflow_t>{a}};