|
Boost Users : |
From: Frederick Virchanza Gotham (cauldwell.thomas_at_[hidden])
Date: 2022-03-16 18:51:56
(Sometimes Gmail changes white space so I hope this post is indented
properly)
I think that "numeric_cast" should be able to cast to and from enum types,
so that it can be used as follows:
#include <cstdint>
#include <iostream>
using std::cout;
using std::endl;
enum class Frog { Blue, Yellow, Pink = 0xFFFFu };
enum class Toad : std::uint8_t { Purple, Orange = 0x7f };
int main(void)
{
try { numeric_cast<Toad>( Frog::Pink ); } catch(std::exception const
&e) { cout << e.what() << endl; }
std::uint32_t my_32_bit_var = 0xffffffffU;
try { numeric_cast<std::uint16_t>(my_32_bit_var); }
catch(std::exception const &e) { cout << e.what() << endl; }
try { numeric_cast<std::uint8_t>( Frog::Pink ); } catch(std::exception
const &e) { cout << e.what() << endl; }
try { numeric_cast<Toad>(0x1FFu); } catch(std::exception const &e) {
cout << e.what() << endl; }
}
In order to achieve this behaviour, I've created my own "numeric_cast" in
the global namespace as follows:
#include <type_traits> // is_enum, underlying_type
#include <boost/numeric/conversion/cast.hpp> // numeric_cast
template<bool T_is_an_enum, typename T>
struct numeric_cast_helper {
typedef T type;
};
template<typename T>
struct numeric_cast_helper<true, T> {
typedef typename std::underlying_type<T>::type type;
};
template<typename A, typename B>
A numeric_cast(B const b)
{
return
static_cast<A>(
boost::numeric_cast<typename
numeric_cast_helper<std::is_enum<A>::value, A>::type>
(
static_cast<typename
numeric_cast_helper<std::is_enum<B>::value, B>::type>(b)
)
);
}
Should the Boost Numeric library be changed so that numeric_cast can work
with enum's?
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net