numeric_cast should work with enum's
(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_caststd::uint16_t(my_32_bit_var); }
catch(std::exception const &e) { cout << e.what() << endl; }
try { numeric_caststd::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
participants (1)
-
Frederick Virchanza Gotham