Date: Sat, 9 Oct 2021 07:36:18 +0300
Hi,
What do you think of the usefulness of two such functions? Do they belong
in the standard library?
template <typename T>
requires std::is_integral_v<T>
constexpr auto as_signed(T v) noexcept
{
return static_cast<std::make_signed_t<T>>(v);
}
template <typename T>
requires std::is_integral_v<T>
constexpr auto as_unsigned(T v) noexcept
{
return static_cast<std::make_unsigned_t<T>>(v);
}
The thing is that the std::container::size() functions return an unsigned
type but we often deal with signed types along with them and the compilers
complain about comparison between these types when warnings are enabled.
Such functions are useful in these situations, in my experience.
Their naming is after std::as_const.
Regards,
Pavel
What do you think of the usefulness of two such functions? Do they belong
in the standard library?
template <typename T>
requires std::is_integral_v<T>
constexpr auto as_signed(T v) noexcept
{
return static_cast<std::make_signed_t<T>>(v);
}
template <typename T>
requires std::is_integral_v<T>
constexpr auto as_unsigned(T v) noexcept
{
return static_cast<std::make_unsigned_t<T>>(v);
}
The thing is that the std::container::size() functions return an unsigned
type but we often deal with signed types along with them and the compilers
complain about comparison between these types when warnings are enabled.
Such functions are useful in these situations, in my experience.
Their naming is after std::as_const.
Regards,
Pavel
Received on 2021-10-08 23:36:30