A prvalue is not a temporary


This is part one of a series of blog posts on temporaries, copies, return value optimization, and passing by value vs. reference.

A good place to start, and the point of this first article, is how a prvalue isn’t necessarily a temporary.

If you’re entirely new to value categories (lvalues, rvalues etc.), you might want to read lvalues, rvalues, glvalues, prvalues, xvalues, help! first.

lvalues vs rvalues

An lvalue is an expression that can not be moved from. An rvalue is an expression that can be moved from.

Let’s first have a look at lvalues. Given this variable v:

std::vector<int> v{1,2,3};

If I now write the expression v somewhere, v is referring to an actual variable. I can’t just move from it, as it would mess up an existing object that someone else could still be using. We call an expression like this an lvalue.

For instance, if I pass my existing vector to a function useVector:

useVector(v);

Here, the expression v is an lvalue, and useVector can’t move from it. After all, someone might want to keep using v on a following line.

But if I know I won’t be needing v anymore, I can turn it into an rvalue, by wrapping it in std::move:

useVector(std::move(v));

Here, the expression std::move(v) is an rvalue. useVector would now be allowed to move from v. (And I must take care to not use v again, since it might have been moved into useVector.)

rvalues: xvalues vs prvalues

Here’s another way to get an rvalue:

useVector(std::vector{1,2,3});

Here, the expression std::vector{1,2,3} is also an rvalue, and again useVector would be allowed to move from it.

Notice, however, that these are two different types of rvalues. std::move(v) takes an existing object and casts it to an rvalue. That type of rvalue is called an xvalue, or “eXpiring lvalue”.

On the other hand, std::vector{1,2,3} is a prvalue, or “pure rvalue”. Unlike an xvalue, this expression never referred to an existing object in the first place. People sometimes call this “a temporary”, but, as is the main point of this article, that’s not necessarily true.

A prvalue in itself is not a temporary. A prvalue is not an object. A prvalue just represents “the idea of the object”, and only turns into a temporary when it absolutely needs to. For instance:

std::vector v = std::vector{1,2,3};

Here, the prvalue std::vector{1,2,3} does not turn into a temporary that is then used to initialize v. Rather, the prvalue is used to initialize v directly, just as if you’d written std::vector v{1,2,3};. No extra temporary is created, and no copies or moves are performed.

Similarly:

void useVector(std::vector<int> v);

useVector(std::vector{1,2,3});

Here, useVector takes its parameter by value. std::vector{1,2,3} never turns into a temporary, the prvalue expression is instead used to initialize the parameter v directly, just like in the previous example. No extra temporary is created, and no copies of moves are performed.

Temporary materialization

However, if useVector takes its parameter by reference:

void useVector(const std::vector<int>& v);

useVector(std::vector{1,2,3});

Now, the reference parameter v needs some object to bind to, and std::vector{1,2,3} actually turns into a temporary object that v can bind to. This is called “temporary materialization”.

Return values

A function call that returns by value is also a prvalue 1. So, given this definition of getVector() and a call to it:

std::vector<int> getVector();

std::vector<int> v = getVector();

Here, the call getVector() is a prvalue which initializes v directly. There is no temporary that is then copied/moved into v. (There might be a copy involved in the return statement inside getVector(), but that’s a story for the next article.)

Conclusion

The point is that a prvalue only materializes into a temporary as a very last resort, avoiding unnecessary copies or moves. Until it needs to materialize, it only represents “the idea of the object”, i.e. what the object would be when it materializes into a temporary or is used to initialize something.

It is important to note that this has nothing to do with optimization. There is no temporary std::vector to optimize away in the first place, there’s just the prvalue, just the “idea of the object”. And then that idea of an object can materialize into an actual object if needed.

Footnotes

  1. §expr.call¶13: “A function call is an lvalue if the result type is an lvalue reference type or an rvalue reference to function type, an xvalue if the result type is an rvalue reference to object type, and a prvalue otherwise.” ↩︎

A taxonomy of C++ types


You may have heard of things like fundamental types, built-in types, basic types, integral types, arithmetic types, and so on. But what do they all mean, if anything?

In this post I’ll gradually build up the hierarchy of C++ types, eventually arriving at a big tree like in the following figure. But I promise we’ll take it easy and gradually, so it all makes sense in the end.

Image
Intentionally unreadable for now, just to show the structure

Woha, that’s a lot! Let’s start with something seemingly simple, like ints.

Integer types

There are five standard signed integer types: “signed char“, "short int“, "int“, “long int“, and “long long int“. The implementation is also allowed to define an arbitrary number of implementation-defined extended signed integer types such as GCC’s __int128.

Together, the standard and extended signed integer types are called the signed integer types. Let’s visualise this:

Image

For each of the standard integer types, there exists a corresponding (but different) standard un-signed integer type. These are “unsigned char“, “unsigned short int“, “unsigned int“, “unsigned long int“, and “unsigned long long int“. And similarly, for each of the extended signed integer types that the implementation defines, it has to define a corresponding extended unsigned integer type. For example, GCC defines unsigned __int128 corresponding to __int128.

Together, the standard and extended unsigned integer types are called the unsigned integer types. Let’s visualise these too, and notice the correspondence to the previous figure:

Image

If we throw in “bool” and the character types “char“, “wchar_t“, “char8_t“, “char16_t“, and “char32_t“, we have all the integral types, also known as the integer types. (Character types have some further subdivisions that are best postponed to a separate blog post.)

Note, by the way, that you can click on all figures in this blog post to expand them.

Image

Floating-point Types

That’s it for the integral types, now for the floating point types. Luckily, this is much simpler, since there’s only “float“, “double“, and “long double“, and they’re all signed. So there are only three standard floating-point types. Then, just as for the integral types, the implementation is allowed to defined extended floating-point types. So collectively we have these floating-point types:

Image

Arithmetic and Fundamental Types

Collectively, the integral and floating-point types form the arithmetic types. Throw in void and std::nullptr_t, and we have all the fundamental types (remember that you can click to expand figures):

Image

This is a good time to ask “But what about built-in and basic types?”. Those are terms that get thrown around in conversations and articles every now and then, but they don’t have an official meaning and are best avoided.

It’s understandable, however, that people refer to the fundamental types as “basic”, since they are indeed very basic compared to other types. They are “just an int”, “just a float” etc. Basically uncomplicated numbers with no additional semantics.

(Note that the term “basic type” was, in fact, used accidentally in a note (but never defined) in the standard until my PR #7287, and should be gone in C++26.)

Compound Types

So if the fundamental types are the simple, basic ones, what are the rest? The rest of the types are the compound types. The word “compound” normally means “made up of two or more parts”, so it makes sense that you find classes, unions, and arrays in this category. However, you also find enums, pointers, references, and functions in this category. I’m not sure why they chose “compound” for these, but that’s the name we have for “all the other types”, or the “non-fundamental types”:

Image

Scalar Types

Finally, I need to mention scalar types, also shown in the diagram above. Luckily for us, no new types are introduced here, scalar types is just a grouping of types we’ve already discussed. The scalar types are all the arithmetic types, plus enums, pointers, and std::nullptr_t.

The reason I bring up scalar types here is that that’s how a memory location is defined in the standard. A memory location is either a scalar type or some bit-field stuff I want to skip for this article. Memory locations are fundamental to understanding the C++ memory model and multi-threaded programming.

Further reading

When writing this post, I considered bringing up integer promotions, conversion and the usual arithmetic conversions, but decided that the post is already long enough. Shafik Yaghmour has a nice post about The Usual Arithmetic Confusions that you might want to check out.

How to become a conference speaker


A reader of my book C++ Brain Teasers recently emailed me and asked:

By the way, I’m curious how one gets to a point in their C++ journey / career when they’re able to give talks at these large conventions. Do you have any insights on this? 

I figured I’d reply in blog form, in case someone else is wondering the same. Note that while I’ve spoken at conferences like CppCon, Meeting C++, and others, I’m by no means a big fish speaker. But I at least have the advantage of explaining it while still remembering how it is to be new in the game.

So, how does one become a speaker at these conventions? Let’s first clarify a few important points:

1. Speakers are not special

Conference speakers might seem like they know more than the rest of us, and one might think only the best programmers become speakers. This is not true. Several of the best programmers I’ve worked with, who are certainly more knowledgable and smarter than me, have never spoken at a conference. To speak at a conference, you don’t need to be best, you just need to have something you want to tell, and be decent at presenting it. (See below if you’re not yet experienced with presenting.)

2. Speakers don’t know it all

When I’m in room A talking about CPU memory models, someone else is in room B talking about coroutines. It’s easy to think that all the speakers know all these things. Most of us don’t. I have no idea how coroutines work. Last week I had to google how to use std::cin.

What I’m saying is that you too can become a speaker, you don’t need to be the smartest or most experienced person in your team / class / group of friends. You just need to love learning, and be motivated to try speaking about what you’ve learned.

Getting started

So how do you get started, then? Speaking is a skill, just like programming. The more you do it, the better you’ll become at it. Here are some tips to get started:

  • If there are tech talks at work, volunteer to give one. If there aren’t, start organizing some. In my experience, management tends to be very happy when someone steps up to improve the skills of the team. Start simple, 10 minute lightning talks before lunch every second week or something. Give the first talk yourself, then ask others to present too. And if your first talk isn’t great, even better, then the bar is lower for the next person! :) The talks can be on something you feel others need to know, something you want to learn about, some language feature, some cool new thing someone did at work, etc.
  • If you’re still in uni/college, volunteer to help your fellow students. This will give you experience with teaching and explaining things. Try to get a job as a teaching assistant.
  • Seek out local user groups. Use a search engine, check meetup.com, etc. In Oslo we have Oslo C++ Users Group, there might be something similar near you. See if they have some lightning talk evenings coming up. If not, message the admin and ask. They tend to like it when people take initiative for something to happen.
  • Be curious! Read, watch other talks, try things out. If you’re not somewhat passionate about this, chances are you won’t enjoy speaking. It’s quite some work, and typically not paid.

But what should I speak about?

Try to focus on something specific for a while

My best trick for coming up with a talk is to work on something specific for a good amount of time. For instance, in once company I worked at, I took the main responsibility for linking and physical architecture. Whenever there was an issue related to linking, symbols, etc., I tried to get myself assigned to it. This taught me a lot, and resulted in three conference talks.

Write a talk about something you want to learn

To be honest, this approach is a lot of work, but it can be a great way to come up with a talk. For instance, at one point I wanted to better understand the Assembly output on https://godbolt.org. I had learned a tiny bit of Assembly in college a long time ago, but had forgotten most of it. I wasn’t able to find a good talk about it, so I figured, hey, let’s learn this, and give that talk that the world is missing. It took weeks, but resulted in my most successful talk Just Enough Assembly for Compiler Explorer, which I’ve presented at many conferences. While it’s a lot more work to write a talk about something you don’t already know well, you have one big advantage – you still know what’s hard about the topic. It’s also fun to learn, and in my experience, the best way to learn something is to attempt to explain it to others.

Talk about what you wish more people knew / did / cared about

This works especially well for lightning talks. Have you recently discovered a super useful tool that more people should know about? Are you driven nuts by people using technique X wrong? Give a lightning talk about it.

Preparing and giving the presentation

There’s a lot to say about preparing and giving a presentation; too much to cover here, and others have done it better. But make sure to prepare well in advance, use high contrast and a large font, and rehearse a lot. Even the most experienced speakers like Matt Godbolt rehearse their talks over and over.

Have fun speaking!

CppQuiz.org is now on C++23


If you’re not familiar with https://cppquiz.org, it is, as its name suggests, a C++ quiz site. Each quiz is a full C++ program, and your task is to figure out what the output is. But the real value often lies in the explanation, which goes into detail about why the answer is what it is. The explanations typically reference the standard quite a lot, so it’s a lot of work to port all of them whenever a new standard is published.

Thanks to some great help from the community, especially @tocic, we managed to port the whole site over the summer. So now you can enjoy up-to-date questions and explanations using the very latest C++23!

As promised in Help Get CppQuiz to C++23 And Win a Book, three contributors were drawn to win a copy of my book C++ Brain Teasers. Below is a recording of that:

Again, thanks a lot to everyone who contributed, and congratulations to the three winners! You will be contacted by email to arrange for shipping.

C++ Brain Teasers Book Launch September 10 (live+streaming)


On September 10 there will be a book launch event for my book C++ Brain Teasers organized by Oslo C++ Users Group at NDC TechTown in Kongsberg. The event starts with food and mingling at 18:00 CEST, and the book launch starts at 18:30. Check out the event at Meetup.com for more details. Update: Here’s the recording of the event.

The event will start with an interview by Olve Maudal, then we’ll do one chapter as a mini-quiz and I will read that chapter. There will be a few physical copies for sale and a discount on the e-book.

I will also attempt to stream the event on YouTube, so if you can’t make it to Kongsberg, head over to my YouTube channel at https://www.youtube.com/@andersknatten and watch it from wherever you are in the world!

I wrote a C++ book!


I’m very proud to announce that my first book just got released on The Pragmatic Programmers! The book is called “C++ Brain Teasers“, and is part of their Brain Teasers series.

The book consists of 25 short C++ programs, and the point is to guess what the output is, and why the language works like that. Much like CppQuiz.org, except with more elaborate and well-written explanations explaining the underlying principles of the language. The puzzles were also selected to be more cohesive and relevant to real-world uses, and the explanations include lots of practical tips to write better and safer code in practice. So the book can be read just for fun or as a deeper learning opportunity, and maybe you can keep a few copies for entertainment around the office?

Image

From the marketing blurb:

C++ is famous for getting all the default behaviors wrong and for sometimes making demons fly out of your nose. Through 25 puzzles, from the useful to the outright weird, we explore some of C++ ‘s most interesting quirks. How does initialization actually work? Do temporaries even exist? Why is +!!”” a valid expression in C++ ? As you work through each puzzle, you will peel off some of the layers of complexity of C++ , getting a fundamental understanding of how the language works. This will help you write better code and recognize issues more easily while debugging.

The book is available both in paperback and as an e-book, and can be purchased from the publisher or wherever you normally buy books. You can even download the preface and three full chapters for free, with no registration needed! I find it especially cool to be published on The Pragmatic Programmers, as “The Pragmatic Programmer” was an important and transformative book early on in my career. I even got support from Dave Thomas himself at one point! 🤩

Finally, a big thank you to Frances Buontempo for introducing me to the publisher, and to my amazing technical reviewers Daniela Engert, Björn Fahller, Olve Maudal, Karthik Nishanth, Tom Schultz, Tina Ulbrich, Sergei Vasilchenko, and Piotr Wierciński!

Happy reading!

Image

Help get CppQuiz to C++23 and win a book!


CppQuiz.org is currently using C++ 17 for explanations and needs porting to C++ 23. I’d really appreciate your help! As a thank you, three contributors will get a copy of my upcoming book C++ Brain Teasers.

How do I help?

All the questions from the site have been exported to https://github.com/knatten/cppquiz23. Full instructions are in the README, but in summary, you just have to pick a question and update the explanation to refer to the C++ 23 standard instead of the C++ 17 one. Usually, there are just a few references that need updating (for instance, something moved from §[basic.type.qualifier]¶6 to §[basic.type.qualifier]¶3), sometimes a bit of rewriting is needed, and sometimes there are no changes at all.

How do I win a copy of the book?

When the porting is done, I randomly pick three of the ported questions that were not ported by me, and the ones who ported those questions get a copy each. If I draw the same person several times, I draw again until I have three separate winners.

Let’s get started!

References don’t have top-level cv-qualifiers


Sometimes when reading about C++, for instance about template argument deduction, the term “top-level cv-qualifiers” comes up. I just spent an unreasonable amount of time being puzzled by something because I didn’t understand that references don’t have top-level cv-qualifiers. This post will hopefully help the next person (hi, next-year Anders) not make the same mistake.

Looking at

const int&

, I assumed without even thinking about it that the const here was the top-level one. It is not.

First, what’s a cv-qualifier? It’s const, volatile or both. Let’s just use const as an example for this article.

Then, what’s a top-level cv-qualifier? The best way to explain is with an example, and the best example is a pointer. There are two levels of constness to a pointer, the constness of the pointer itself, and the constness of what it’s pointing to.

Given const int *, a non-const pointer to const int, we can visualise it as

pointer (the * part)
to
const int (the const int part)

And given const int * const, a const pointer to const int, we can visualise it as

const pointer (the * const part)
to
const int (the const int part)

(And so on, you can imagine how it looks for pointers to non-const int.) The top-level cv-qualifier is the one on the top level, the cv-qualifier on the pointer itself.

Now, how does this look for references?

Given const int&, a reference to const int, we can visualise it as

reference (the & part)
to
const int (the const int part)

But there’s no such thing as a const reference! Constness applies to the object itself, and a reference is not an object, just an alternative name for an existing object. So there is no such thing as a const int& const, i.e. there’s no such thing as

const reference (the & const part)
to
const int (the const int part)

Which means, references don’t have top-level cv-qualifiers. The standard even has an example:

Example: The type corresponding to the type-id const int& has no top-level cv-qualifiers.

[basic.type.qualifier]

This is by the way a somewhat recent addition, until this Core Language Defect Report was resolved in 2014, the term “top-level cv-qualifier” was never actually defined in the standard.

Why we probably shouldn’t have constexpr conditional operator


The idea

I had a great idea. We have constexpr if, but no constexpr conditional operator. Time for a proposal?

Since we can do stuff like this:

if constexpr(cond) { foo; } else { bar;}

Wouldn’t it be cool if we could also do

cond ? constexpr foo : bar;

My motivation was that I had a std::variant visitor that was identical for all types except one. So instead of writing an overload set for std::visit, it was simpler to have one common lambda with a conditional inside. Something like this, which returns “int” for int and “other” for all other types in the variant:

std::visit([]<typename T>(T value) {
        if constexpr(std::is_same_v<int, T>)
        {
            return "int";
        }
        else
        {
            return "other";
        }
    },
    my_variant);

It would be nicer to write it like this with a conditional operator, but now we can’t use constexpr.

std::visit([]<typename T>(T value) {
        return std::is_same_v<int, T> ? "int" : "other";
    },
    my_variant);

So I had the idea of constexpr conditional operator, so I could write my lambda something like this:

std::visit([]<typename T>(T value) {
         return std::is_same_v<int, T> ? constexpr "int" : "other";
    },
    my_variant);

In this case, constexpr doesn’t actually make much of a difference. std::is_same_v is a constant expression no matter if you use the constexpr keyword or not, so the compiler optimises it equally well in either case. But at least we verify that the condition is actually a constant expression. If we mess this up, we get a compiler error.

But the most important advantage of constexpr if is that each branch only need to compile if that branch is taken at compile time. So you can do for instance

template<typename T>
int f(T t) {
    if constexpr(std::is_same_v<T, std::string>)
        return t.size();
    else
        return t;
}

and this will work both for int and std::string, even if the first branch wouldn’t compile for an int and the second wouldn’t compile for std::string. Remove constexpr above, and you’re in trouble.

As it turns out, this is exactly why constexpr conditional operator might not be such a good idea! Thanks to Daniela Engert who pointed this problem out to me.

The problem

if is a statement, it doesn’t have a type. The conditional operator however is an expression, and has a type!

You can’t assign the result of an if statement to something, it doesn’t have a type or result in a value. The conditional operator does however. And the type of the conditional operator is determined by a set of rules which find a common type for the two branches. For instance:

auto result = false ? std::optional<int>{2} : 0;

The two branches have the types std::optional<int> and int, respectively. The compiler now has to figure out what the type of the expression should be, by trying to form implicit conversion sequences from the first to the other, and vice versa. See [expr.cond] for details. Since one can implicitly convert an int to a std::optional<int>, but not vice versa, the type of the full conditional expression (and thus the type of result) is std::optional<int>.

If we introduced something like ? constexpr here, with the same semantics as if constexpr, suddenly one of the branches would be discarded. And we’d have to do that, since the whole point is that the branch not taken usually doesn’t even compile. So in the case above, the first branch would be discarded, and we’d only be left with the literal 0 which has type int. Left with only the int to deduce a type from, the type of the full conditional expression would now be int instead of std::optional<int>. And Daniela’s argument, which I agree with, is that it could be surprising if the type of an expression changed just by introducing or removing constexpr.

In comparison, remember that an if statement doesn’t result in a value, and doesn’t even have a type. If you want to do the same with an if, you first have to define the result variable, and there’s no way to do that upfront without explicitly deciding on its type:

std::optional<int> result;
if constexpr (false)
    result = std::optional<int>{2};
else
    result = 0;

Notice here that the type of the value we assign to result is still different based on the constexpr condition, but now there’s no surprise, the resulting type is always the same. Both branches have to result in a type implicitly convertible to std::optional<int>, if they’re ever instantiated.

A counter argument?

There is one final point that needs to be mentioned, where the types of two if constexpr branches actually do influence type deduction. This can happen when you have a function with an auto return type, and you return from inside the if constexpr. Here’s a demonstration with a function template, but it can also happen for regular functions:

template<bool b>
auto f()
{
    if constexpr (b)
        return std::optional<int>{2};
    else
        return 0;
}

The return type of f<true> is std::optional<int>, and the return type of f<false> is int. Isn’t this the same problem we just used to argue against constexpr conditional operator? It’s similar, but not the same. The big difference is that removing constexpr in this example doesn’t change the deduced type, it rather causes a compilation error. This is due to dcl.spec.auto#8, which is very strict about all non-discarded return statements having the same type, not just types that can be implicitly converted to a common type:

If a function with a declared return type that contains a placeholder type has multiple non-discarded return statements, the return type is deduced for each such return statement. If the type deduced is not the same in each deduction, the program is ill-formed.

dcl.spec.auto#8

Conclusion

For constexpr conditional operator, adding/removing constexpr could change a deduced type, which could be surprising. For constexpr if, this doesn’t happen.

What do you think? Should we have constexpr conditional operator or not?