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!
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!
Is this code example:
... is there meant to be a declaration of the variable "color" somewhere? Or, did you mean to use "c"?Code:Color c = Color::GREEN; if ( Color::RED == color ) { // the color is red }
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
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>.
@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.
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.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.
@whiteflags stdint.h is a good point, but it's part of C99, not C++ (not even C++11!)
Mention <cstdint> thenOriginally Posted by webmaster
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Ah, thanks--somehow I missed cstdint being added in C++11!
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
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); //}
Thanks for the great feedback--I've updated the article to talk a bit about cstdint and also use std::nullptr_t
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