Why I like C++ 11 – auto & nullptr

I’ve been away from C++ for the last three years, this is the longest we’ve been apart.
Luckily I’ve been given a chance to sharpen my skills and in the last few weeks I’ve been working exclusively in C++.
During our time apart C++ has grown, and changed – this is not your father’s C++ it’s the new and improved C++ 11!
In fact C++ 11 seems like a completely different language – and I’m not the only one that think that:
C++11 feels like a new language: The pieces just fit together better than they used to and I find a higher-level style of programming more natural than before and as efficient as ever.
Bjarne Stroustrup from C++ 11 FAQ
So I invite you to join me as I re-discover C++. Today I’ll discuss two new keywords in C++ 11 – auto and nullptr:


auto enable us to write code that looks like this:
auto i = 5;    // I'm an integer
auto s = "text";    // I'm a char*
auto vec = new vector<double>(); // and I'm vector<double>*

Did C++ just became dynamic? Of course not!
Auto is similar to C#’s var keyword – it tells the compiler to go ahead and figure out what type I want you to use – because the compiler works for us and not the other way around.
Although I’ve seen auto (and var) abused if used correctly – this simple trick can save you some time  especially when using with lambdas (new feature) or just plain Iterator<x<y<z … which happen from time to time.


No much to tell about this – but I’ll try: nullptr replace NULL when comparing or setting pointer values. The big difference is that NULL of old was plain ‘0’ (zero) in disguise causing funny (not “ha-ha” funny)  behavior from 0 == NULL to accidently calling the wrong overload of a method – func(int) vs. func(MyClass*).
Using nullptr (of type std::nullptr_t) – if you want more details have a look at this StackOverflow question.

So far so good

Is this all – not by a long shot. C++11 have many other goodies – which I plan to write about in future blog posts.

Happy coding…

Labels: ,