Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > "enum" vs. "enum class"

Reply
Thread Tools

"enum" vs. "enum class"

 
 
Ike Naar
Guest
Posts: n/a
 
      08-27-2012
On 2012-08-27, Ansel <(E-Mail Removed)> wrote:
> So then, it *doesn't* work like the old enum. Do you see my original
> question now? Maybe they should have left the old enum alone (?). The old
> enum is gone in C++ 11 and now there are 2 new enum constructs. So then, the
> *new* "old style" enum implicitly converts to int?


The old enum isn't gone.
Below is a short description of the new enums in C++ 2011.

http://www.stroustrup.com/C++11FAQ.html#enum

 
Reply With Quote
 
 
 
 
Ansel
Guest
Posts: n/a
 
      08-27-2012
Öö Tiib wrote:
> On Monday, August 27, 2012 9:48:18 AM UTC+3, Ansel wrote:
>> ?? Tiib wrote:
>>> Such forward declaration is allowed:
>>> enum Selection : unsigned char;
>>>
>>> The article calls it "traditional" but actually it is new syntax.

>>
>> The "traditional" part is referring to the use of 'enum' instead of
>> 'enum class'. 'enum' is the traditional construct wordage while
>> 'enum class' is the C++ 11 construct wordage.

>
> OK.
>
>>> Such enum works like old enum but underlying type is user-specified.

>>
>> So then, it *doesn't* work like the old enum. Do you see my original
>> question now?

>
> No. The old enum had implementation-specified underlying type. So it
> was (at some cases aggravatingly) dim for developer. The code that
> did manage to work with old enum still works.
>


OK. (I knew that).

>> Maybe they should have left the old enum alone (?). The old
>> enum is gone in C++ 11 and now there are 2 new enum constructs.

>
> Nothing is gone. Why you say it? There are now 3 enums,


Ah yes! So there are! I stand corrected.

> the one
> exactly as in C++2003, the "traditional" with underlying type
> specified and the totally new 'class enum'.
>


A sad state of affairs for those learning to program and using C++ do learn
with. One example, then, of C++ not being good for that. (That was an aside,
but noteworthy I think).

>> So then, the *new* "old style" enum implicitly converts to int?

>
> Yes. The 'class enum' alone is not converting implicitly.


Was the in-between one really necessary?

> The
> compilers may warn about others if they want to but have to convert.



 
Reply With Quote
 
 
 
 
Ansel
Guest
Posts: n/a
 
      08-27-2012
Ike Naar wrote:
> On 2012-08-27, Ansel <(E-Mail Removed)> wrote:
>> So then, it *doesn't* work like the old enum. Do you see my original
>> question now? Maybe they should have left the old enum alone (?).
>> The old enum is gone in C++ 11 and now there are 2 new enum
>> constructs. So then, the *new* "old style" enum implicitly converts
>> to int?

>
> The old enum isn't gone.


Oo Tiib mostly cleared it up on his last post.

> Below is a short description of the new enums in C++ 2011.
>
> http://www.stroustrup.com/C++11FAQ.html#enum


A table is appropriate:

Feature enum 2003 ('enum') enum Bastard ('enum :') enum 2011
('enum class :')

Can specify underlying type? N Y Y
Prevents implicit conversion to 'int'? N N Y
Prevents export of enumerators? N N Y


 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      08-27-2012
On 2012-08-27, Ansel <(E-Mail Removed)> wrote:
> Ike Naar wrote:
>> On 2012-08-27, Ansel <(E-Mail Removed)> wrote:
>>> So then, it *doesn't* work like the old enum. Do you see my original
>>> question now? Maybe they should have left the old enum alone (?).
>>> The old enum is gone in C++ 11 and now there are 2 new enum
>>> constructs. So then, the *new* "old style" enum implicitly converts
>>> to int?

>>
>> The old enum isn't gone.

>
> Oo Tiib mostly cleared it up on his last post.
>
>> Below is a short description of the new enums in C++ 2011.
>>
>> http://www.stroustrup.com/C++11FAQ.html#enum

>
> A table is appropriate:
>
> Feature enum 2003 ('enum') enum Bastard ('enum :') enum 2011
> ('enum class :')
>
> Can specify underlying type? N Y Y
> Prevents implicit conversion to 'int'? N N Y
> Prevents export of enumerators? N N Y


Have you read the FAQ? If not, please do, it may clear up
some misunderstanding.
There is nu such thing as enum Bastard.
There are enum and enum class,
both can optionally have the underlying type specified.
That makes four combinations:

enum Color {red, green};
enum Color : unsigned char {red, green};
enum class Color {red, green};
enum class Color : unsigned char {red, green};

The first one is the old-style enum.
 
Reply With Quote
 
Ansel
Guest
Posts: n/a
 
      08-27-2012
Ike Naar wrote:
> On 2012-08-27, Ansel <(E-Mail Removed)> wrote:
>> Ike Naar wrote:
>>> On 2012-08-27, Ansel <(E-Mail Removed)> wrote:
>>>> So then, it *doesn't* work like the old enum. Do you see my
>>>> original question now? Maybe they should have left the old enum
>>>> alone (?). The old enum is gone in C++ 11 and now there are 2 new
>>>> enum constructs. So then, the *new* "old style" enum implicitly
>>>> converts to int?
>>>
>>> The old enum isn't gone.

>>
>> Oo Tiib mostly cleared it up on his last post.
>>
>>> Below is a short description of the new enums in C++ 2011.
>>>
>>> http://www.stroustrup.com/C++11FAQ.html#enum

>>
>> A table is appropriate:
>>
>> Feature enum 2003 ('enum') enum Bastard ('enum :') enum
>> 2011 ('enum class :')
>>
>> Can specify underlying type? N Y Y
>> Prevents implicit conversion to 'int'? N N Y
>> Prevents export of enumerators? N N Y

>
> Have you read the FAQ? If not, please do, it may clear up
> some misunderstanding.
> There is nu such thing as enum Bastard.
> There are enum and enum class,
> both can optionally have the underlying type specified.
> That makes four combinations:
>
> enum Color {red, green};
> enum Color : unsigned char {red, green};
> enum class Color {red, green};
> enum class Color : unsigned char {red, green};
>
> The first one is the old-style enum.


I think my table sums it up quite nicely against the key operational
criteria, and there are really only 3 distinct constructs (the underlying
type being an option in enum 2012, and what defines enum Bastard), not 4.
Feel free to enter my table into the FAQ.


 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      08-27-2012
On 2012-08-27, Ansel <(E-Mail Removed)> wrote:
> Ike Naar wrote:
>>>>
>>>> http://www.stroustrup.com/C++11FAQ.html#enum
>>>

> I think my table sums it up quite nicely against the key operational
> criteria, and there are really only 3 distinct constructs (the underlying
> type being an option in enum 2012, and what defines enum Bastard), not 4.


So for "enum class" (and "enum struct") you consider the underlying
type specification to be an option, while for "enum" you're
considering it to be a distinct construct? It's your choice, of course,
but it goes against the syntax for enums in C++2011 (section 7.2),
which treats the underlying type specification in the same
way for "enum" / "enum class" / "enum struct".

(For a draft version of C++2011, see

http://www.open-std.org/jtc1/sc22/wg...2011/n3242.pdf

).

> Feel free to enter my table into the FAQ.


You'd have to ask Bjarne Stroustrup. It's his FAQ.
 
Reply With Quote
 
Ansel
Guest
Posts: n/a
 
      08-27-2012
Ike Naar wrote:
> On 2012-08-27, Ansel <(E-Mail Removed)> wrote:
>> Ike Naar wrote:
>>>>>
>>>>> http://www.stroustrup.com/C++11FAQ.html#enum
>>>>

>> I think my table sums it up quite nicely against the key operational
>> criteria, and there are really only 3 distinct constructs (the
>> underlying type being an option in enum 2012, and what defines enum
>> Bastard), not 4.

>
> So for "enum class" (and "enum struct") you consider the underlying
> type specification to be an option, while for "enum" you're
> considering it to be a distinct construct?


It's fuzzy, because you can't have C++ 03 without have an
underlying-type-optionless enum.

> It's your choice, of
> course, but it goes against the syntax for enums in C++2011 (section
> 7.2),


But the perspective given (the context) was across C++ versions.

> which treats the underlying type specification in the same
> way for "enum" / "enum class" / "enum struct".
>
> (For a draft version of C++2011, see
>
> http://www.open-std.org/jtc1/sc22/wg...2011/n3242.pdf
>
> ).
>
>> Feel free to enter my table into the FAQ.

>
> You'd have to ask Bjarne Stroustrup. It's his FAQ.


Ask? I was offering. Bjarne, feel free to use "my" enum table in your FAQ.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments