C++17 brings a lot of cool new features. Viva64 have a nice overview. Sadly, they can’t all be used out of the box on macOS, as the default toolchain isn’t very modern.

The problem

Apple installs their own version of llvm along with Xcode. This custom llvm also uses custom version numbers, so it isn’t always easy to know which version of mainline llvm it is based on. For instance on my macOS High Sierra, I get:

$ clang --version
Apple LLVM version 9.0.0 (clang-900.0.39.2)

This version number has nothing to with the official llvm version, which is currently at 6.0.0. All I know is that it is behind.

The solution(s)

If you want to try out some new features today, you have a few options:

Use std::experimental to play with certain features

Install a more modern llvm

Using std::experimental

Sometimes, the C++ standards committee publishes experimental C++ language and library extensions for future standardization. These go in the std::experimental namespace. If you just want to play with some new features, your toolchain might already include what you’re looking for here. However, be aware that these features as they exist in the experimental namespace are not guaranteed to be identical to how they end/ended up in the actual C++ standard! So this should just be used for toying around, not for production code.

As an example, I wanted to try out the new optional feature. This was included in a technical specification, and has been available as std::experimental::optional for a while. Note however that its interface changed before being merged to the official standard as std::optional. The former for instance lacks the has_value member function.

Installing a more modern llvm

This is the proper solution. It’s not too hard either! First, you probably don’t want to risk breaking your current Xcode setup by installing a mainline llvm interfering with the Apple llvm used by Xcode. Luckily, homebrew avoids this problem by default, by installing llvm in another location than the Apple llvm, and not adding the new one to path.

This will take quite some time, and you’ll end up with mainline llvm (currently version 6.0.0) in a path you can find by running:

$ brew --prefix llvm

On my machine, I get:

/usr/local/opt/llvm

I’m not going to copy paste their entire guide, so I suggest you read through it for the details of how to configure your project to use the newly installed mainline llvm instead of the apple llvm you have in path.

Did you use to have some sort of intuition for what “lvalue” and “rvalue” meant? Are you confused about glvalues, xvalues and prvalues, and worry that lvalues and rvalues might also have changed? This post aims to give you a basic intuition for all five of them.

First, a warning: This post does not aim to give a complete definition of the five value categories. Instead, I only hope to give a basic intuition, which I hope will help to have in the back of your mind next time you need to look up the actual details of one of them. Feel free to flame me in the comment section, but I don’t promise I’ll bite! :)

Back before C++11, there were two value categories, lvalue and rvalue. The basic intuition was that lvalues were things with identities, such as variables, and rvalues were temporaries:

However, along came rvalue references and move semantics. On the surface, the old lvalue / rvalue distinction seems sufficient: Never move from lvalues (people might still be using them), feel free to move from rvalues (they’re just temporary anyway):

Why did I put “Can’t move” and “lvalue” in red in that diagram? It turns out that you might want to move from certain lvalues! For instance, if you have an lvalue you won’t be using anymore, you can std::move() it to cast it to an rvalue reference. A function can also return an rvalue reference to an object with identity.

So as it turns out, whether something has identity, and whether something can be moved from, are orthogonal properties! We’ll solve the problem of moving from lvalues soon, but first, let’s just change our diagram to reflect our new orthogonal view of the world:

Clearly, there’s a name missing in the lower left corner here. (We can ignore the top right corner, temporaries which can’t be moved from is not a useful concept.)

C++11 introduces a new value category “xvalue”, for lvalues which can be moved from. It might help to think of “xvalue” as “eXpiring lvalue”, since they’re probably about to end their lifetime and be moved from (for instance a function returning an rvalue reference).

In addition, what was formerly called “rvalue” was renamed to “prvalue”, meaning “pure rvalue”. These are the three basic value categories:

But we’re not quite there yet, what’s a “glvalue”, and what does “rvalue” mean these days? It turns out that we’ve already explained these concepts! We just haven’t given them proper names yet.

A glvalue, or “generalized lvalue”, covers exactly the “has identity” property, ignoring movability. An rvalue covers exactly the “can move” property, ignoring identity. And that’s it! You now know all the five value categories.

As usual on CppQuiz, the objective is to figure out the output of the program, according to the C++ standard. Before continuing, please try to find the answer yourself!

What does the standard say?

Did you reply bdB? So did the person who contributed the question, and I initially went “yep, that seems logical”. It does indeed seem logical that these functions are called, but what exactly happens when we fail to destroy the derived part of an object? Let’s have a look at §5.3.5/3 in the C++11 standard:

if the static type of the object to be deleted is different from its dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted and the static type shall have a virtual destructor or the behavior is undefined.

The static type here is B, which is indeed a base class of the dynamic type D. The static type B does however not have a virtual destructor, so the behavior is undefined. (I have yet to see a compiler who doesn’t print bdB though.)

So why is this undefined behavior? It would be hard for the standard to specify exactly what happens when an object is not properly destroyed.

In practice, most compilers will probably go with the normal rules for virtual functions, print bdB, and simply not destroy the D part of the object. But since the behavior is undefined, there’s no guarantee for this.

One could argue that even if the result of not destroying D properly is undefined, the standard could still mandate which destructor(s) actually get called. However, when undefined behavior occurs in a program, the entire execution is undefined, so there really would be no point.

If you’re curious about undefined behavior, I’ve written about it before. That article also has links to some really interesting, more in-depth articles about the subject.

You might be familiar with my C++ quiz site, cppquiz.org. But did you know you can contribute your own questions to the site? First, try solving a few questions to get the gist of it, then visit http://cppquiz.org/quiz/create to create your own.

I try to live by the “quality over quantity” motto for the questions on the site, so you’ll find the following guidelines on the submission form:

Your question should be short, and demonstrate one thing only

Your question should compile (unless not compiling is the point)

Your question should be about standard C++, not compiler specific or about 3rd party libraries

Your question should not be a trick question, and be free from distractions

Your explanation should be clear and to the point

Your explanation should use correct terminology, and refer to the standard where possible

Prefer well defined programs over programs with compilation errors, undefined or unspecified behaviour

But what does all of that mean?

Your question should be short, and demonstrate one thing only

The shorter the code in the question, the better. The point of a question is to teach one single aspect of C++, not to be an exercise in reading and understanding an unfamiliar code base.

Your question should compile (unless not compiling is the point)

It should be possible to copy your code verbatim and have it compile as C++11, without errors, requirements for additional includes etc. (Of course, some questions are not intended to compile, then this rule does not apply.)

Your question should be about standard C++, not compiler specific or about 3rd party libraries

This should be fairly self explanatory, don’t make questions about posix, boost, windows.h etc.

Your question should not be a trick question, and be free from distractions

I’ve refused several questions due to this. Your question is supposed to be non-trivial, but the difficulty should be to understand a concept of C++, not to read the question correctly, or find the clue in the midst of distractions. Don’t make questions where the key is noticing that the variables v1 and vl are not the same. Don’t make questions full of complicated C++ that doesn’t matter, but is just there to hide the simple core of the question.

Your explanation should be clear and the point

Don’t go on lengthy asides in the explanation. As your question is already short and demonstrates one thing only, explaining that single concept should not take too many words.

Your explanation should use correct terminology, and refer to the standard where possible

The chances of having your question published quickly increases if you use correct C++ terminology to describe the concepts in your question. It helps me a lot if you’re also able to provide references to the standard, but don’t let this stop you from contributing.

Prefer well defined programs over programs with compilation errors, undefined or unspecified behaviour

There’s a lot of fun and interesting things to learn from questions that don’t compile, or contain undefined or unspecified behaviour. It’s however not so fun when most of the questions on the site can be answered simply by enumerating those three alternatives as an answer.

That’s it! I hope I didn’t scare you away from contributing. Please have a go, and don’t be afraid to ask if you have any questions about your question! :)

As I’ve previously posted about, there was a great C++ track at NDC this year. It turns out that videos of all the talks are now out. Big thanks to Olve Maudal for putting together this track! And thanks to all the speakers for interesting talks and some nice chats.

As someone who doesn’t do web development, and didn’t use to do any .net, I’ve not always been too excited by the Norwegian Developers Conference agenda. This year however, I’m very impressed by the C++ track organized by Olve Maudal et. al.

See the full agenda at ndcoslo.com, especially Wednesday and Thursday. I’ll be talking about ints. If you think that sounds like a narrow topic, rest assured there’ll be a char too! Oh, and a secret tip, if you’re a member of Oslo C++ Users Group, contact me for a discount!

Today was my first day at my new job as a staff engineer at Outracks Technologies. Outracks is a small startup in Oslo creating Uno, the world’s first hybrid CPU/GPU programming language, and Realtime Studio, a powerful, easy-to-use IDE for real-time graphics in 2D and 3D. In other words, theywe’re the coolest technology company in Norway! :)

For a one-minute intro to Realtime Studio, check out the brand new product video. Today also marked the release of the Faraday milestone release, which comes with a video of its own. If you want to check out Uno and Realtime Studio, don’t hesitate to sign up for the closed beta!

Discovering the demoscene back in 1995 was the main reason I got into programming in the first place, and with this job it really feels like I’ve come full circle.

Since Uno is not C++ (even though it compiles down to it on several of our target platforms), I will probably not post much more about it here. If you’re interested in keeping up, you can follow me and Outracks on Twitter, and like Outracks on Facebook.