static_assert in templates


Quiz time! Which of the following programs can you expect to not compile? For bonus points, which are required by the C++ standard to not compile?

Program 1

struct B{};

template <typename T>
struct A {
//Assume sizeof(B) != 4
static_assert(sizeof(T) == 4);
};

A<B> a;

Program 2

struct B{};

template <typename T>
struct A {
//Assume sizeof(B) != 4
static_assert(sizeof(T) == 4);
};

A<B>* a;

Program 3

struct B{};

template <typename T>
struct A {
//Assume sizeof(int) != 4
static_assert(sizeof(int) == 4);
};

In case you’re not familiar with static_assert, it takes a constant boolean expression, and if it evaluates to false, you get a compilation error. The most basic example is just doing static_assert(false). If you have that anywhere in your program, compilation fails. But let’s start at the beginning:

Program 1

struct B{};

template <typename T>
struct A {
//Assume sizeof(B) != 4
static_assert(sizeof(T) == 4);
};

A<B> a;

Here we have a class template struct A, which takes a type T as its single template parameter. We then assert that the size of the provided template argument is 4.

We then define a variable a of type A<B>. In order to do that, we need the complete definition of A<B>, so that specialization of A gets implicitly instantiated. In that specialization, sizeof(T) becomes sizeof(B), which is not equal to 4, and compliation fails.

Program 2

struct B{};

template <typename T>
struct A {
//Assume sizeof(B) != 4
static_assert(sizeof(T) == 4);
};

A<B>* a;

This is the exact same problem as in Program 1, except we only define a pointer to A<B>. Does this result in a implicit instantiation? Let’s have a look at [temp.inst] (§17.7.1) ¶1 in the C++17 standard:

Unless a class template specialization has been explicitly instantiated (17.7.2) or explicitly specialized (17.7.3), the class template specialization is implicitly instantiated when the specialization is referenced in a context that requires a completely-defined object type or when the completeness of the class type affects the semantics of the program.

The class template specialization A<B> has not been explicitly instantiated nor explicitly specialized, so the question is then whether it’s implicitly instantiated. We’re only declaring a pointer to it, which doesn’t require a completely-defined object type, so it’s not instantiated. The program compiles just fine.

Program 3

struct B{};

template <typename T>
struct A {
//Assume sizeof(int) != 4
static_assert(sizeof(int) == 4);
};

In this variation, we’re asserting on the size of int, rather than the size of the template argument. And given the assumption that sizeof(int) != 4, that assertion will always fail. However, we’re never actually instantiating any specialization of A whatsoever. In Program 2, not instatiating the template allowed us to ignore the static_assert. Does the same apply here? In fact, it doesn’t. Let’s have a look at [temp.inst] (§17.6) ¶8 in the C++17 standard:

The program is ill-formed, no diagnostic required, if:

[…]

a hypothetical instantiation of a template immediately following its definition would be ill-formed due to a construct that does not depend on a template parameter

The static_assert(sizeof(int) == 4) does not depend on a template parameter, so if we were to instantiate A immediately following its definition, A would always be ill-formed.

So our program is ill-formed, no diagnostic required.

Now what does that mean? ill-formed is the terminology used by the standard for a program that’s not valid C++, where compilation is required to fail with an error message. Ill-formed, no diagnostic required however means our program is not valid C++, but that the compiler isn’t required to let us know. The standard makes no guarantees on the behaviour of the program, i.e. we have undefined behaviour.

So Program 3 has undefined behaviour. In practice however, both Clang, gcc and msvc gives a compilation error in this case.

Summary

Program Because … … the standard says In practice
1 We need the class definition Compilation error Compilation error
2 We don’t need the class definition No error No error
3 The assertion doesn’t depend on T Undefined behaviour Compilation error

If you enjoyed this post, you can subscribe to my blog, or follow me on Twitter.

+!!””


In this Tweet, @willkirkby posts:

+!!””

that evaluates as 1 in C/C++, but no, JavaScript is the weird language

C++ is indeed weird, or at least it’s very weakly typed. Let’s go through all the details of what’s going on here:

+!!""

Summary:

Starting from the right, "" is a string literal, which gets converted to a pointer, which again gets converted to a bool with the value true. This then gets passed to two operator!s, which flip it to false and back to true again. Finally, operator+ converts the bool true to the int 1.

All of this happens behind our backs, so to speak, as C++ is very eager to do conversions we didn’t explicitly ask it to do. This eagerness to do implicit conversions is by the way why you should always mark your single argument constructors and your converting operators explicit.

Detailed explanation:

Now let’s go through this in detail, quoting the C++17 standard. Starting from the right:

"" is a string literal. [lex.string]¶8:

A narrow string literal has type “array of n const char

Then comes operator!. We have an array of n const char, can we use that for operator!? [expr.unary.op]¶9 says:

The operand of the logical negation operator ! is contextually converted to bool (Clause 7); its value is true if the converted operand is false and false otherwise. The type of the result is bool.

So we need to contextually convert our array of n const char to bool. [conv]¶5 says:

Certain language constructs require that an expression be converted to a Boolean value. An expression e appearing in such a context is said to be contextually converted to bool and is well-formed if and only if the declaration bool t(e); is well-formed, for some invented temporary variable t.

So let’s see where bool t(e); takes us when e is an array of n const char.[conv]¶2:

expressions with a given type will be implicitly converted to other types in several contexts:

  • When used as the source expression for an initialization

In our case, the expression is of type “array of n const char“, and we need a bool. We’re going to need a standard conversion sequence. [conv]¶1:

A standard conversion sequence is a sequence of standard conversions in the following order:

  • (1.1) Zero or one conversion from the following set: lvalue-to-rvalue conversion, array-to-pointer conversion, and function-to-pointer conversion.
  • (1.2) Zero or one conversion from the following set: integral promotions, floating-point promotion, integral conversions, floating-point conversions, floating-integral conversions, pointer conversions, pointer to member conversions, and boolean conversions.— […]

So we can first use an array-to-pointer conversion (1.1) to get from “array of n const char” to a pointer. We can then use a boolean conversion (1.2) to get from pointer to bool.

First, the array-to-pointer conversion, [conv.array]¶1:

An lvalue or rvalue of type “array of N T” […] can be converted to a prvalue of type “pointer to T”. The result is a pointer to the first element of the array.

So we now have a pointer to the first element (the terminating \0).

And then the boolean conversion [conv.bool]¶1:

A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

Since our pointer is not a null pointer, its value converts to true.

Negating this twice with two operator!s is trivial, we end up back at true.

Finally, true is passed to operator+. [expr.unary.op]¶7:

The operand of the unary + operator shall have arithmetic, unscoped enumeration, or pointer type and the result is the value of the argument.

bool is not an arithmetic type, so we need to promote the bool true to an arithmetic type before passing it to opreator+. [conv.prom]¶6:

A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one.

So the bool true becomes the int 1, and we’re done.

If you enjoyed this post, you can subscribe to my blog, or follow me on Twitter.

Why you can’t list-initialize containers of non-copyable types


Have you ever wondered why you can’t list-initialize containers of non-copyable types? This is for instance not possible:

    vector<unique_ptr<int>> vu{
        make_unique<int>(1), make_unique<int>(2)};
    //error: call to implicitly-deleted copy constructor of unique_ptr

If you ever wondered, or if you now are, read on!

List-initialization

Since C++11, you’re probably used to intitalizing containers like this:

    vector<int> vi1{1,2,3};
    vector<int> vi2 = {1,2,3};

This of course also works with user defined types. Let’s say you have a class Copyable, then you can for instance do:

    Copyable c1(1);
    Copyable c2(2);
    vector<Copyable> vc1{c1, c2};
    vector<Copyable> vc2 = {c1, c2};

(Copyable is just an arbitrary class which can be copied. It’s reproduced at the end of the post.)

Now what happens if we have a non-copyable class NonCopyable? (NonCopyable is just an arbitrary class which can be moved but not copied, it too is reproduced at the end of the post.)

    NonCopyable n1(1);
    NonCopyable n2(2);
    vector<NonCopyable> vn1{n1, n2}; //error: call to deleted constructor of 'const NonCopyable'
    vector<NonCopyable> vn2 = {n1, n2}; //error: call to deleted constructor of 'const NonCopyable'

Well, n1 and n2 are lvalues, so no wonder it tries to copy them. What if we turn them into rvalues, either with std::move or by creating temporaries?

    vector<NonCopyable> vn3{std::move(n1), std::move(n2)}; //error: call to deleted constructor of 'const NonCopyable'
    vector<NonCopyable> vn3{NonCopyable(4), NonCopyable(5)}; //error: call to deleted constructor of 'const NonCopyable'

So what’s going on here, why is it trying to copy our rvalues? Let’s see what the standard has to say in [dcl.init.list]¶1:

List-initialization is initialization of an object or reference from a braced-init-list.

A braced-init-list is the {element1, element2, ...} syntax we saw above. The standard continues:

Such an initializer is called an initializer list. (…) List-initialization can occur in direct-initialization or copy-initialization contexts.

So list-initialization applies both to the forms vector<Copyable> vc1{c1, c2} and vector<Copyable> vc2 = {c1, c2}, which we saw above. The former is an example of direct-initialization, the latter of copy-initialization. In both cases, {c1, c2} is the braced-init-list.

(Note that the word copy-initialization here is not what causes a copy. Copy-initialization simply refers to the form T t = expression, which doesn’t necessarily invoke the copy constructor.)

Creating the initializer_list

Now what exactly happens with the braced_init_list, and how do its elements end up inside the container we’re initializing?

[dcl.init.list]¶5

An object of type std::initializer_list<E> is constructed from an initializer list as if the implementation generated and materialized (7.4) a prvalue of type “array ofN const E“, where N is the number of elements in the initializer list. Each element of that array is copy-initialized with the corresponding element of the initializer list, and thestd::initializer_list<E> object is constructed to refer to that array.

So the initializer_list can be thought of as just a wrapper for a temporary array we initialize with the elements in the braced-init-list. Sort of like if we’d been doing this:

    const Copyable arr[2] = {c1, c2};    
    vector<Copyable> vc3(initializer_list<Copyable>(arr, arr+2));

Consuming the initializer_list

Now that our initializer_list has been created and passed to the vector constructor, what can that constructor do with it? How does it get the elements out of the initializer_list and into the vector?

[initializer_list.syn] lists the very sparse interface of std::initializer_list:

constexpr const E* begin() const noexcept; // first element
constexpr const E* end() const noexcept; // one past the last element

There’s no access to the elements as rvalue references, only iterators of pointers to const, so we only get lvalues, and we need to copy. Why is there no access as rvalue references?

As we saw in the quote above, “the std::initializer_list<E> object is constructed to refer to that array.” So it only refers to it, and does not own the elements. In particular, this means that if we copy the initializer_list, we do not copy the elements, we only copy a reference to them. In fact, this is spelled out in a note [initializer_list.syn]¶1:

Copying an initializer list does not copy the underlying elements.

So even if we get passed the initializer_list by value, we do not get a copy of the elements themselves, and it would not be safe to move them out, as another copy of the initializer_list could be used again somewhere else. This is why initializer_list offers no rvalue reference access.

Summary

In summary: When you do T t{elm1, elm2}, an initializer_list is created, referring to those elements. Copying that initializer_list does not copy the elements. When a constructor takes an initializer_list, it does not know whether it’s the only consumer of those elements, so it’s not safe to move them out of the initializer_list. The only safe way to get the elements out is by copy, so a copy constructor needs to be available.

As usual, the code for this blog post is available on GitHub.

If you enjoyed this post, you can subscribe to my blog, or follow me on Twitter.

Appendix: The Copyable and Uncopyable classes:

class Copyable {
public:
    Copyable(int i): i(i){}
    Copyable(const Copyable&) = default;
    Copyable(Copyable&&) = default;
    Copyable& operator=(const Copyable&) = default;
    Copyable& operator=(Copyable&&) = default;
    ~Copyable() = default;
    int i;
};

class NonCopyable {
public:
    NonCopyable(int i): i(i){}
    NonCopyable(const NonCopyable&) = delete;
    NonCopyable(NonCopyable&&) = delete;
    NonCopyable& operator=(const NonCopyable&) = default;
    NonCopyable& operator=(NonCopyable&&) = default;
    ~NonCopyable() = default;
    int i;
};

CppQuiz Android App now available!


CppQuiz.org is an open source C++ quiz site ran by me, with contributions from the C++ community. If you’re unfamiliar with it, you can read more in its “About” section.

A few weeks ago, I was contacted by Sergey Vasilchenko. Without my knowledge, he’d built a prototype Android app for CppQuiz, and asked for my input. Naturally I was delighted, and since then we’ve been collaborating a bit on the project. I’ve been making some adjustments to the backend (basically adding some API functionality to cppquiz.org) and I’ve also done a bit of testing and review of the app. Most of the work here was by Sergey of course, who implemented the app.

The app works very similarly to the site itself, with the big advantage that it also works when you’re off-line. It downloads all the questions from cppquiz.org on startup, and updates its database regularly. Perfect for your daily, bad reception commute!

Currently training mode is the only available mode, but Sergey is working on a quiz mode too. He’s also planning to open source the app in the near future.

So, go download the C++ Quiz App now, and give it a spin!

If you enjoyed this post, you can subscribe to my blog, or follow me on Twitter.

CppQuiz.org now uses C++17


CppQuiz.org is an open source C++ quiz site ran by me, with contributions from the C++ community. If you’re unfamiliar with it, you can read more in its “About” section.

Thanks to great help from Nicolas Lesser and Simon Brand, all the questions and explanations on CppQuiz.org have now been updated from C++11 to C++17. This task would have taken me a very long time to do on my own, but thanks to Nicolas and Simon, this was all done in no more than four days!

If you want to test your knowledge of C++, try out the quiz now!

If you enjoyed this post, you can subscribe to my blog, or follow me on Twitter.

Help save CppQuiz.org! We’re porting to C++17.


[start helping now]

CppQuiz.org is an open source C++ quiz site ran by me, with contributions from the C++ community. If you’re unfamiliar with it, you can read more in its “About” section

All the CppQuiz questions are currently targetting C++11. We need to update them for C++17. Most questions will still have the same answers, we just need to update the explanations and references to the standard. A few questions will also have different answers.

Doing this all by myself is going to take months, so I would very much appreciate some help from the community. Everyone who helps will be credited in a blog post when the porting is done.

To make porting as simple as possible, I’ve created a repository containing all the questions. There is a directory for each question, named after the question number. That directory contains the source code of the question in a .cpp file, the hint and explanation in .md files, as well as a README.md explaining everything you need to do to port the question. There’s also an issue for each question, making it easier to track progress. The issue has the same information as the README.md file.

As soon as we’ve updated all the questions in this repository, I’ll import them back into CppQuiz, and from then on CppQuiz will always ask about C++17.

How to help porting questions

There are two ways to contribute, listed below. I prefer the first alternative, but if that prevents you from contributing, the second is also ok.

Contributing using a fork and pull requests

  1. Fork the repo by clicking the “Fork” button at the top right.
  2. Pick the issue for a question you want to port. Add a comment that you’ll be handling that issue.
  3. Follow the instructions in the issue to port the question.
  4. Make a pull request. Porting several questions in one PR is fine.

Contributing without a fork

If you think forking and PRs is too cumbersome, or you are not able to do this for other reasons, I’d still appreciate your help:

  1. Pick the issue for a question you want to port. Add a comment that you’ll be handling that issue.
  2. Follow the instructions in the issue to port the question.
  3. Paste the updated files as comments to the issue.

Other ways to help

  • Look for questions labeled “help wanted”. It means the person responsible needs a second pair of eyes.
  • Look at pull requests, review them and comment “LGTM” if they should be merged.
  • Other ideas for help are also welcome, please get in touch (see “Questions” below).

Questions

If you have any questions, either file an issue in the repo, contact @knatten on Twitter, or email me at anders@knatten.org.

[start helping now]