
By defn, static_cast does not provide runtime checks. You should use an enum class or something. Boost needs scoped enums! Anyway, I've used this with varying degrees of success but its not really easy to specialize: #include <cassert> struct MyEnum { enum type { a,b,c, min=a, max=c }; }; template<typename Enum> typename Enum::type convert_enum(int a) { typedef typename Enum::type type_; assert(a >= Enum::min && a <= Enum::max); return type_(a); } int main() { MyEnum::type b(convert_enum<MyEnum>(0)); // ok MyEnum::type a(convert_enum<MyEnum>(5)); // dies }
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Oliver.Kowalke@infineon.com Sent: Sunday, March 26, 2006 10:42 PM To: boost-users@lists.boost.org Subject: [Boost-users] safe assign of int to enum
Hello, is a technique available in boost in order to assign an int value to a enum?
enum E { e1 = 1, e2 = 2, e3 = 3 };
E e( static_cast< E >( 1) ); // should work
E e( static_cast< E >( 5) ); // should throw exception at runtime
Regards, Oliver _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users