October 13, 2015 - Daniel Kolesa

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:

becomes

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:

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:

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

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:

Static assert

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

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:

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.

Daniel Kolesa

About Daniel Kolesa

Daniel Kolesa has been contributing to open source since 2007 and has worked with a handful open source projects. His work has been primarily on Enlightenment, starting in late 2008 with his main focus being the Enlightenment Foundation Libraries. In January 2014 he moved from the Czech Republic to the UK and joined the Samsung Open Source Group to focus more on the EFL and eventually became a maintainer of several core EFL components including Eolian and Elua. He is also a video game developer, maintaining his own OctaForge 3D engine. FInally, he is also a functional programming fan, having worked with languages such as Rust and OCaml, and having designed his own programming languages and compiler tech.

Image Credits: Tom Small

Development c++ / c++11 / Clang / GCC / programming /

Comments

  • Angel G. says:

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments Protected by WP-SpamShield Anti-Spam