Introduction to C++ 11 Series – Part 4, Strongly Typed and Scoped enums

March 24, 2013

If you are familiar with C#/.NET enums, you know how safer and easier it is to have scoped enums. Like C#, the new C++ enums are scoped, and you can choose the underlined integral type for the enum.

 

Old C++ enum:

 

enum old_enum

{

       old_value

};

 

The new syntax: (similar to the C# syntax)

 

enum newer_enum : unsigned long long

{

       newer_value = ~(unsigned long long)0

};

 

The new enum provides:

  • enumerators are in the scope of their enum (VS2012)
  • enumerators and enums do not implicitly convert to int
  • enums have a defined underlying type
  • Pure backward-compatible extensions for plain enums
    • The ability to specify the underlying type
    • The ability to qualify an enumerator with the name of the
      enum

Here is the complete sample:

void enum_sample()

{

       enum old_enum

       {

              old_value

       };

 

       enum newer_enum : unsigned long long

       {

              newer_value = ~(unsigned long long)0

       };

 

       cout << "Old size: " << sizeof(old_value) << endl;

       cout << "Newer size: " << sizeof(newer_value) << endl;

 

       cout << "Enum scope: " << (unsigned long long)newer_enum::newer_value << endl;

}

The result:

clip_image001

 

Forward Declaration enum

One last enum improvement is the ability to forward declare them. You can declare the enum with its underlined type, use it in other declaration such as function argument type or a class field, and provide the enum members later on, before you use them. This is a valid code now:

 

enum forward_declaration : unsigned char;

void foo(forward_declaration f);

 

enum forward_declaration: unsigned char

{

       val1 = 1,

       val2 = 2

};

 

void use_forward_declaration_enum(forward_declaration f)

{

       cout << f << endl;

}

 

void forward_declaration_enum_sample()

{

       use_forward_declaration_enum(forward_declaration::val2);

}

 

You can read more about it here.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

7 comments

  1. projekty domówMay 8, 2013 ב 16:25

    I seriously delight in your posts. Thank you projekty domów

    Reply
  2. peloEmegoHofeMay 9, 2013 ב 06:19

    An impressive share, I just given this onto a colleague who was performing just a little analysis on this. And he actually bought me breakfast given that I found it for him.. smile. So let me reword that: Thnx for the treat! But yeah Thnkx for spending the time to discuss this, I feel strongly about it and adore reading a lot more on this topic. If possible, as you become expertise, would you mind updating your weblog with extra details? It truly is highly valuable for me. Big thumb up for this blog post!

    [URL=http://www.buysredbottomshoes.com]christian louboutin shoes[/URL]

    Reply
  3. rooleatlyMay 15, 2013 ב 07:56

    The next time I read a blog, I hope that it doesnt disappoint me as significantly as this one. I mean, I know it was my selection to read, but I truly thought youd have some thing intriguing to say. All I hear is often a bunch of whining about something which you could fix in case you werent too busy seeking for attention.

    [URL=http://www.lululemononforcheap.com]cheap lululemon[/URL]

    Reply
  4. joxBloopyMay 21, 2013 ב 16:37

    Precisely every legacy and new-look IT vendor has its own consume on making the entire data center more programmable via software and less dependent on specialized, proprietary and over the odds hardware.

    Reply
  5. joxBloopyMay 21, 2013 ב 16:37

    Nearly every legacy and new-look IT vendor has its own consume on making the whole data center more programmable via software and less dependent on specialized, proprietary and pricey hardware.

    Reply
  6. unknownSeptember 8, 2013 ב 02:51

    Introduction to C 11 Series – Part 4, Strongly Typed and Scoped enums – class Alon : public CPP, public Architecture

    Reply