Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Efficient E...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 13 Topic 9544 of 10094
Post > Topic >>

Re: Efficient Enum Access...

by =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@[EMAIL PROTECTED] Apr 26, 2008 at 04:43 AM

On 25 Apr., 23:48, Thiago Adams <thiago.ad...@[EMAIL PROTECTED]
> wrote:
> Integers are not directly convertible to enums, for instance:
>
> enum E { e1,  e2 };
>
> E e = 1; // error: cannot convert from 'int' to 'E'
>
> Generally people use static_cast like this:
>
> E e = static_cast<E>(1);
>
> it works, but it is not save.

This is safe in this particular example, but of-course
not in the general case.

> So, my suggestion is to create a similar (but safe) cast called
> "enum_cast"
>
> E e = enum_cast<E>(1);
>
> The implementation would be like this:
>
> //primary template (not implemented)
> template <class T1, class T2> T1 enum_cast(T2);
>
> // and someone must provide the specialization
> template <> E enum_cast(int v)

Why only int as source type?

> {
>    E e = static_cast<E>(v);
>    if (e != e1 || e != e2 )
>      throw std::range_error("invalid convertion from 'int' to 'E'");
>    return e;
>
> };

This definition of enum_cast would break valid
programs, because it's absolutely fine to cast
an integral value into the complete range of the
underlying type of the enumeration as described
by [dcl.enum]/5+6.
An IMO more reasonable definition would just
check whether the given source value is within
the limits

[std::numeric_limits<underlying type of E>::min(),
std::numeric_limits<underlying type of E>::max()]

Unfortunately there exists no general way for
a programmer to determine the exact underlying type
of a classical enumeration type, but it is possible
via a not-too-complicated machinery to find an integral
type that has the same size as it and on the other
hand to find it's integral promotion type (as defined
by [conv.prom]/2).

HTH & Greetings from Bremen,

Daniel Krügler


-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 13 Posts in Topic:
Efficient Enum Access...
Thomas Lehmann <t.lehm  2008-04-25 10:48:42 
Re: Efficient Enum Access...
nickf3 <nickf3@[EMAIL   2008-04-25 15:47:51 
Re: Efficient Enum Access...
Thiago Adams <thiago.a  2008-04-25 15:48:52 
Re: Efficient Enum Access...
brangdon@[EMAIL PROTECTED  2008-04-26 08:47:57 
Re: Efficient Enum Access...
=?ISO-8859-1?Q?Daniel_Kr=  2008-04-26 04:43:31 
Re: Efficient Enum Access...
brangdon@[EMAIL PROTECTED  2008-04-26 08:47:32 
Re: Efficient Enum Access...
=?ISO-8859-1?Q?Daniel_Kr=  2008-04-26 16:31:39 
Re: Efficient Enum Access...
brangdon@[EMAIL PROTECTED  2008-04-27 12:39:04 
Re: Efficient Enum Access...
=?ISO-8859-1?Q?Daniel_Kr=  2008-04-28 01:55:51 
Re: Efficient Enum Access...
Seungbeom Kim <musiphi  2008-04-26 08:42:16 
Re: Efficient Enum Access...
Alberto Ganesh Barbati &l  2008-04-26 08:43:12 
Re: Efficient Enum Access...
Tony Delroy <tony_in_d  2008-04-28 02:13:22 
Re: Efficient Enum Access...
Marsh Ray <marsh527@[E  2008-05-02 12:48:54 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Oct 11 18:23:25 CDT 2008.