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

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

    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,817
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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!

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    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
    1,012
    @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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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
    1,012
    @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
    28,413
    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
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    1,012
    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,895
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    1,012
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

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, 04: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, 08:21 PM
  5. enums!
    By newjamie in forum C Programming
    Replies: 1
    Last Post: 03-13-2002, 12:25 PM