Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   "enum" vs. "enum class" (http://www.velocityreviews.com/forums/t951400-enum-vs-enum-class.html)

Ansel 08-26-2012 11:31 AM

"enum" vs. "enum class"
 
Are there additional semantics when using "enum class" than when using
"enum" in C++ 11, or are they exactly the same?



Bo Persson 08-26-2012 06:48 PM

Re: "enum" vs. "enum class"
 
Ansel skrev 2012-08-26 13:31:
> Are there additional semantics when using "enum class" than when using
> "enum" in C++ 11, or are they exactly the same?
>
>


Yes, lots. Why else would someone introduce a new construct?

http://en.wikipedia.org/wiki/C++11#S...d_enumerations


Bo Persson


Ansel 08-27-2012 03:10 AM

Re: "enum" vs. "enum class"
 
Bo Persson wrote:
> Ansel skrev 2012-08-26 13:31:
>> Are there additional semantics when using "enum class" than when
>> using "enum" in C++ 11, or are they exactly the same?
>>
>>

>
> Yes, lots. Why else would someone introduce a new construct?
>
> http://en.wikipedia.org/wiki/C++11#S...d_enumerations
>
>


I wasn't asking about the new construct though, but rather if the old one
works just like the new one now. IOW, is the word "class" unnecessary?



Tiib 08-27-2012 03:45 AM

Re: "enum" vs. "enum class"
 
On Monday, August 27, 2012 6:10:41 AM UTC+3, Ansel wrote:
> Bo Persson wrote:
> > Ansel skrev 2012-08-26 13:31:
> >> Are there additional semantics when using "enum class" than when
> >> using "enum" in C++ 11, or are they exactly the same?

> >
> > Yes, lots. Why else would someone introduce a new construct?
> >
> > http://en.wikipedia.org/wiki/C++11#S...d_enumerations

>
> I wasn't asking about the new construct though, but rather if the old one
> works just like the new one now. IOW, is the word "class" unnecessary?


Old syntax works the way it did.

New standards always avoid breaking behavior of legacy code unless it is impossible. It was impossible with 'auto' keyword (but no one used in old sense anyway). 'enum' is used in a lot of code so that they do not dare to modify it ever.


Ansel 08-27-2012 05:00 AM

Re: "enum" vs. "enum class"
 
Tiib wrote:
> On Monday, August 27, 2012 6:10:41 AM UTC+3, Ansel wrote:
>> Bo Persson wrote:
>>> Ansel skrev 2012-08-26 13:31:
>>>> Are there additional semantics when using "enum class" than when
>>>> using "enum" in C++ 11, or are they exactly the same?
>>>
>>> Yes, lots. Why else would someone introduce a new construct?
>>>
>>> http://en.wikipedia.org/wiki/C++11#S...d_enumerations

>>
>> I wasn't asking about the new construct though, but rather if the
>> old one works just like the new one now. IOW, is the word "class"
>> unnecessary?

>
> Old syntax works the way it did.
>
> New standards always avoid breaking behavior of legacy code unless it
> is impossible. It was impossible with 'auto' keyword (but no one used
> in old sense anyway). 'enum' is used in a lot of code so that they do
> not dare to modify it ever.


From
http://www.codeguru.com/cpp/cpp/arti...ped-Enums.htm:

"However, it is possible to specify the underlying type for traditional
enums too. This is also legal in C++ 2011 (and actually supported for some
years by various compilers):

enum Selection : unsigned char
{
None,
Single,
Multiple,
};

Forward declaration with traditional enums will also be possible"

Comment (anyone) on the above please.



Juha Nieminen 08-27-2012 05:51 AM

Re: "enum" vs. "enum class"
 
Ansel <tinker454@trytospammenowloser.com> wrote:
> "However, it is possible to specify the underlying type for traditional
> enums too. This is also legal in C++ 2011 (and actually supported for some
> years by various compilers):
>
> enum Selection : unsigned char
> {
> None,
> Single,
> Multiple,
> };
>
> Forward declaration with traditional enums will also be possible"
>
> Comment (anyone) on the above please.


I don't understand what you are asking.

Ansel 08-27-2012 06:11 AM

Re: "enum" vs. "enum class"
 
Juha Nieminen wrote:
> Ansel <tinker454@trytospammenowloser.com> wrote:
>> "However, it is possible to specify the underlying type for
>> traditional enums too. This is also legal in C++ 2011 (and actually
>> supported for some years by various compilers):
>>
>> enum Selection : unsigned char
>> {
>> None,
>> Single,
>> Multiple,
>> };
>>
>> Forward declaration with traditional enums will also be possible"
>>
>> Comment (anyone) on the above please.

>
> I don't understand what you are asking.


Did you read the thread from the beginning? (It's all in the post you
replied to). From what you snipped above, it appears you didn't, for that is
just what I copied from another website. In my last post, I wrote, "Comment
(anyone) on the above please.", meaning in the context of the whole thread.
Oo Tiib said: "New standards always avoid breaking behavior of legacy code
unless it is impossible.". So is the website article wrong in saying that
enums without the 'class' keyword can have the base type specified, and
such? It all goes back to what I asked in my OP.



Tiib 08-27-2012 06:28 AM

Re: "enum" vs. "enum class"
 
On Monday, August 27, 2012 9:11:09 AM UTC+3, Ansel wrote:
> Juha Nieminen wrote:
> > Ansel <tinker454@trytospammenowloser.com> wrote:
> >> "However, it is possible to specify the underlying type for
> >> traditional enums too. This is also legal in C++ 2011 (and actually
> >> supported for some years by various compilers):
> >>
> >> enum Selection : unsigned char
> >> {
> >> None,
> >> Single,
> >> Multiple,
> >> };
> >>
> >> Forward declaration with traditional enums will also be possible"
> >>
> >> Comment (anyone) on the above please.

> >
> > I don't understand what you are asking.

>
> Did you read the thread from the beginning? (It's all in the post you
> replied to). From what you snipped above, it appears you didn't, for that is
> just what I copied from another website. In my last post, I wrote, "Comment
> (anyone) on the above please.", meaning in the context of the whole thread.
> Oo Tiib said: "New standards always avoid breaking behavior of legacy code
> unless it is impossible.". So is the website article wrong in saying that
> enums without the 'class' keyword can have the base type specified, and
> such? It all goes back to what I asked in my OP.


Such forward declaration is allowed:
enum Selection : unsigned char;

The article calls it "traditional" but actually it is new syntax. Such enum works like old enum but underlying type is user-specified.

Such forward declaration is not allowed:
enum Selection;

It was not allowed before.

Even if the new standard allowed both forward declarations (or some compiler allows it as extension) it would not break any existing code. That's why it is hard to understand what you ask.

Ansel 08-27-2012 06:48 AM

Re: "enum" vs. "enum class"
 
Tiib wrote:
> On Monday, August 27, 2012 9:11:09 AM UTC+3, Ansel wrote:
>> Juha Nieminen wrote:
>>> Ansel <tinker454@trytospammenowloser.com> wrote:
>>>> "However, it is possible to specify the underlying type for
>>>> traditional enums too. This is also legal in C++ 2011 (and actually
>>>> supported for some years by various compilers):
>>>>
>>>> enum Selection : unsigned char
>>>> {
>>>> None,
>>>> Single,
>>>> Multiple,
>>>> };
>>>>
>>>> Forward declaration with traditional enums will also be possible"
>>>>
>>>> Comment (anyone) on the above please.
>>>
>>> I don't understand what you are asking.

>>
>> Did you read the thread from the beginning? (It's all in the post you
>> replied to). From what you snipped above, it appears you didn't, for
>> that is just what I copied from another website. In my last post, I
>> wrote, "Comment (anyone) on the above please.", meaning in the
>> context of the whole thread. Oo Tiib said: "New standards always
>> avoid breaking behavior of legacy code unless it is impossible.". So
>> is the website article wrong in saying that enums without the
>> 'class' keyword can have the base type specified, and such? It all
>> goes back to what I asked in my OP.

>
> 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.

> 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? 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?

>
> Such forward declaration is not allowed:
> enum Selection;
>
> It was not allowed before.
>
> Even if the new standard allowed both forward declarations (or some
> compiler allows it as extension) it would not break any existing
> code. That's why it is hard to understand what you ask.




Tiib 08-27-2012 07:08 AM

Re: "enum" vs. "enum class"
 
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.

> 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, the one
exactly as in C++2003, the "traditional" with underlying type
specified and the totally new 'class enum'.

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


Yes. The 'class enum' alone is not converting implicitly. The compilers
may warn about others if they want to but have to convert.


All times are GMT. The time now is 01:33 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.