Some Final Thoughts on C++11

This article is part 4 of a 4 part series on the benefits of the C++11 revision of the C++ programming language:

The previous three articles in this series introduced readers to C++11 toolchains and some of the biggest features this revision of C++ provides. This article covers a handful of minor features that are still quite valuable for anyone writing software in C++11 including range-based for loops, initializer lists, null pointer constants, strongly typed enumerations, static asserts, user defined literals and more. Finally, this article closes with an introduction to a personal project of mine that makes a handful of things in C++11 easier.

Other improvements

There are quite a few features of C++11 that are worth mentioning, even if they don’t warrant as detailed an entry as the last two articles in this series.

Range-based for Loop

There is now a new for loop syntax for iterating over ranges. It’s designed to work with the STL iterator system, which is a shame, but you can always make a wrapper iterator to make it work with your own types. Anyway, when working with the STL, this:

for (auto it = something.begin(); it != something.end(); ++it) {


for (auto &v: something) {

Initializer Lists

The expression { a, b, c, ... } now defines a value of type std::initializer_list. Keep in mind that all elements need to be the same type. It’s then possible to retrieve a pointer to those elements from the initializer list. This allows for neat initialization of containers, such as:

std::vector<int> x = { 5, 10, 15, 20 };

Null Pointer Constant

C++11 finally fixes the whole null pointer mess. As NULL is defined to be the constant 0 in C++, having two overloads like this:

void test(int *);
void test(int);

will always trigger the latter when called with NULL, which is probably not what you intended; the new constant nullptr fixes this, so you never have to use NULL anymore. This constant’s type is nullptr_t which is builtin (you can retrieve it using decltype) and you can also make explicit overloads for this type.

Strongly Typed Enumerations

You can now say

enum class Something {
    foo, bar, baz

Something x = Something::bar;

and it means that foo, bar and baz will never implicitly convert to an integral type (hence strongly typed), instead you always have to refer to them as of type Something. You can also specify the integral base for the enum, by default it’s int:

enum class Something: unsigned char { ... };

Static assert

You can now assert at compile-time. The syntax is:

static_assert(cond, "error message");

C++17 will make the error message optional.

User defined literals

It’s now possible to define custom literals for strings, integers and floats. An user defined literal is a suffix. For example for strings, you can define one as:

MyString operator "" _mystr(const char *str, size_t len) {
    return MyString(str, len);

auto x = "test"_mystr;

There are several other forms of user defined literals as well – look them up if you’re interested.

And others

Numerous other features were added. Examples include thread local storage, raw string literals, object alignment control/query, noexcept, attributes, as well as many improvements in the standard library.

Final Words

I’m currently working on a library called OctaSTD in my free time. It’s essentially an implementation of a new standard library replacement that incorporates some novel concepts such as ranges, as well as older stuff like zero-overhead string slices and extensive string formatting. You can take a look if you want more research material, inspiration for your own code, or if you possibly might be interested in helping ;-).

Unfortunately it’s not possible to cover everything C++11 offers with this series, but I hope you found this information interesting and will consider using C++11 in your existing or new C++ projects. I tried to explain (in my opinion, at least) the most interesting stuff as well as clear up some common confusion points.

If you have any questions or feedback, don’t hesitate to leave a comment.

Author: Daniel Kolesa

Daniel has been contributing to open source since 2007 and has been involved with the EFL and the OctaForge 3D game engine in addition to other projects.

One thought on “Some Final Thoughts on C++11”

  1. I think that the more simple a language is – the better.
    1. Easier for new people to learn it
    2. Easier for more people to understand what’s going on
    3. Less in-brain cache memory necessary to read it.
    4. Fewer possible constructs -> fewer possibilities for mistakes.
    So I’d recommend to avoid any new strange constructs when writing open source…
    Even a+=b; is better written as: a=a+b, because requires less brains to read.

Comments are closed.