New C++11 article - strongly typed enums and nullptr

This is a discussion on New C++11 article - strongly typed enums and nullptr within the C++ Programming forums, part of the General Programming Boards category; The latest article in the C++11 series, this one covering enum classes and nullptr: http://www.cprogramming.com/c++11/c+...num-class.html Feedback, as always, welcome!...

  1. #1
    Administrator webmaster's Avatar
    Join Date
    Aug 2001
    Posts
    963

    New C++11 article - strongly typed enums and nullptr

    The latest article in the C++11 series, this one covering enum classes and nullptr:
    http://www.cprogramming.com/c++11/c+...num-class.html

    Feedback, as always, welcome!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Is this code example:
    Code:
    Color c = Color::GREEN;
    if ( Color::RED == color )
    {
        // the color is red
    }
    ... is there meant to be a declaration of the variable "color" somewhere? Or, did you mean to use "c"?



    These examples duplicate each other and come quite close together. The later one can probably be removed.
    Enter strongly typed enums--and I don't mean enums. Strongly typed enums are a new kind of enum, declared like so:

    Code:
    // this code will compile (if your compiler supports C++11 strongly typed enums
    enum class Color {RED, GREEN, BLUE};
    enum class Feelings {EXCITED, MOODY, BLUE};
    ...

    This means that code like this will compile:

    Code:
    // this code will compile
    enum class Color {RED, GREEN, BLUE};
    enum class Feelings {EXCITED, MOODY, BLUE};
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    Just want to point out that I really like it when you push out articles describing the new features in C++11. Keep it up!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Quote Originally Posted by Elysia View Post
    Just want to point out that I really like it when you push out articles describing the new features in C++11. Keep it up!
    Me too..
    Hope something about Multi-threading will be ready soon.
    I looked at some video lectures... but nothing beats a long and 'boring' article that can be leisurely read!
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,144
    Quote Originally Posted by manasij7479 View Post
    Hope something about Multi-threading will be ready soon.
    the C++11 threading features are very similar to the boost::thread library. any boost::thread tutorial will give you a pretty good start. I've used the new threading stuff a bit in some of my code, and it seems to be pretty straightforward. there are a few things that you have to pay special attention to. watch out for the std::this_thread namespace. a few of the functions are disabled by default, unless you #define certain macros before including <thread>.

  6. #6
    Administrator webmaster's Avatar
    Join Date
    Aug 2001
    Posts
    963
    @hk_mp5pdw Thanks, good suggestions! I've fixed the code example and removed the duplicate

    @Elysia and manasij7479 Thanks, it's really nice to hear that! I'm starting background research for the multithreading article(s) now but it may take some time to really get everything into a form I'm happy with.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,525
    Well-defined enum sizes

    A final advantage of enum classes is that you can set the size of your enum--you can use any signed or unsigned integer type. It defaults to int, but you can also use char, unsigned long, etc. This will ensure some measure of compatibility across compilers (although you still must be on guard against the possibility of differently sized values on different architectures, such as whether int is 32-bit or 64-bit). But at least you have control rather than being stuck with the choice made by your compiler vendor.
    I'm not trying to say this is wrong, but this part means you could also use stdint.h to get really specific sizes, so I don't like the wording. I think we should mention stdint.h somewhere.

  8. #8
    Administrator webmaster's Avatar
    Join Date
    Aug 2001
    Posts
    963
    @whiteflags stdint.h is a good point, but it's part of C99, not C++ (not even C++11!)

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by webmaster
    stdint.h is a good point, but it's part of C99, not C++ (not even C++11!)
    Mention <cstdint> then
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Administrator webmaster's Avatar
    Join Date
    Aug 2001
    Posts
    963
    Ah, thanks--somehow I missed cstdint being added in C++11!

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Writing decltype(nullptr) everytime is verbose and not very clear. It's better to use std::nullptr_t.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,159
    The C++ standardized threading support is pathetic and about 15 years too late. Just the fact that it exposes a raw "thread" object instead of using real tasks and task scheduling is a sign that they missed the boat. Using threads and mutexes directly is the concurrency equivalent of writing your entire program in assembly language.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    Administrator webmaster's Avatar
    Join Date
    Aug 2001
    Posts
    963
    Thanks for the great feedback--I've updated the article to talk a bit about cstdint and also use std::nullptr_t

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    Quote Originally Posted by webmaster View Post
    @Elysia and manasij7479 Thanks, it's really nice to hear that! I'm starting background research for the multithreading article(s) now but it may take some time to really get everything into a form I'm happy with.
    Hmmm.
    Just watch out for std::thread. It's broken. It does not even support proper RAII schematics, so actually using it can be a pain.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    brewbuck: You have to walk before you can run. You can build a task library on top of raw threads. The other way round might be possible, but is not very useful.
    I agree with "damn late" though.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Library Implementation of nullptr
    By laserlight in forum C++ Programming
    Replies: 1
    Last Post: 12-27-2007, 03:14 PM
  2. printing enums
    By xddxogm3 in forum C++ Programming
    Replies: 19
    Last Post: 08-09-2005, 12:08 PM
  3. Strongly typed collections
    By CompiledMonkey in forum C++ Programming
    Replies: 2
    Last Post: 06-04-2004, 09:25 PM
  4. Limiting the number of characters typed
    By johnnyd in forum C Programming
    Replies: 10
    Last Post: 04-03-2002, 07:21 PM
  5. enums!
    By newjamie in forum C Programming
    Replies: 1
    Last Post: 03-13-2002, 11:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21