|
Boost : |
From: Frederick Virchanza Gotham (cauldwell.thomas_at_[hidden])
Date: 2022-03-16 15:12:07
(Sometimes Gmail takes out white space so I'm hoping 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 Toad : std::uint8_t { Purple, Orange = 0x7f };
enum class Frog { Blue, Yellow, Pink = 0xFFFFu };
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) // This function takes its argument by
value, and also returns by value -- which is fine for arithmetic types
{
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)
)
);
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk