Some basic C++0x; only the last line is valid C++

I'm quite a fan of C++. It gets the job done, it's fairly easy to comprehend from a logical view, and it provides total control over everything you need to do. I like Python for different reasons; it's very flexible, i.e. in terms of variable types and syntax.

x = 5;
x = 5.5;
x = "5";
x = [5,5.5,"5"];

The above code is a completely valid set of four Python statements. Now let's look at how you'd have to do it in C++...

int x1 = 5;
double x2 = 5.5;
const char *x3 = "5";
const char *x4[] = {"5","5.5","5"};

Note that the last line isn't even possible in C++ (we just used strings instead); you cannot have an array containing multiple different data types without some more complex class techniques (which need not be covered here).

However, the new standard for C++, which is due probably sometime in 2011, can do all of that.

auto x1 = 5; // Is 'int'
auto x1 = 5.5; // Is 'double'
auto x1 = "5"; // Is 'const char*'
std::vector<auto> x2 = {5,5.5,"5"}; // This is a new variable, not an 'auto'

Note: Not totally sure if the last line will be correct; but if not you just create a class with an auto type variable and use that as the vector type.

There are numerous new features being introduced in C++0x, some of which already exist in C++ as a result of converting to the new standard, but here are some of the better ones.

  • Auto Variable Type - The example above.
  • Range-based For Loop - Two examples, both C++0x valid, however the second is not valid in current C++.
int my_array[5] = {1, 2, 3, 4, 5};
for(unsigned int x = 0; i < _countof(my_array); x++) {
    my_array[x] *= 2;

int my_array[5] = {1, 2, 3, 4, 5};
for(int& x : my_array) {
    x *= 2;

Though on the surface, you might ask, "What's the point?" Well, programmers are lazy. Any excuse to make a shorter code is a good excuse. :)

  • Lambda Functions - This probably isn't worth going into a huge amount of detail here... if you're super interested, see the link at the end of the blog for more.

  • Object Construction - Currently, C++ constructors cannot call other C++ constructors. For example, the following code is C++0x-valid but not C++-valid.

    class Vector2D {
        float x;
        float y;
        Vector2D(float newX, float newY) { x = newX; y = newY; }
        Vector2D() : Vector2D(0.0f,0.0f) {};
  • Null Pointer Constant - Previously, we've checked if some object pointer we have is NULL (which is a constant with a value of zero) to see if it's a null pointer. Now, however, we can just compare against the new constant nullptr that will only work on pointers. For example, if you have an integer (that isn't a pointer) and compare it to nullptr, the compiler would spit out an error. This is just a little semantic thing that kind of completes the idea of a null pointer.

  • Right Shift Syntax Fix - Compare the following two lines of code; the first is not legal syntax, the other is. (Assume we have template<bool bTest> class SomeType;.)

    std::vector<SomeType<1>2>> x1;
    std::vector<SomeType<(1>2)>> x1;
  • String Literals - Currently, in C++, these are a pain in the ass. But allowing things like u8"\\u27Hello\\u27", which is equivalent to 'Hello' (including the quotes), is something that was awkward and far from envious in C++.

  • Thread-local Storage - Again, not worth going into detail; see link at the end.

  • Tuples - Tuples are kind of like arrays, that store different types of data. Here's an example of tuple usage from Wikipedia's article on C++0x:

    typedef tuple< int, double, long &, const char * > test_tuple ;
    long lengthy = 12 ;
    test_tuple proof( 18, 6.5, lengthy, "Ciao!" ) ;
    lengthy = get<0>(proof) ;  // Assign to 'lengthy' the value 18.
    get<3>(proof) = " Beautiful!" ;  // Modify the tuple's fourth element.
  • New Random Generator - The std:: C++ library will now use the Mersenne Twister random algorithm rather than the current, undesirable, outdated algorithm.

I could go on for hours, listing off great features of C++0x that I can't wait to use in application programming. Here is the aforementioned link that a lot of this information is from and that lists the upcoming features of C++0x.

Regardless, I think that the features I've mentioned here are more than enough reason to hope and pray that C++0x is in the near future.

Next Post Previous Post