C++ Compile-Time Exceptions

Debugging C++ templates should be less surreal and horrible

Patrick Bos
Netherlands eScience Center
9 min readJan 23, 2019

C++ programmers in their natural habitat, building awesome things and staring at walls of error text.

A C++ template error can be pages long, really dense and basically horrible. Often, they are not at all helpful for users of a library and you have to ask the developers for help. Developers will often be able to filter or modify the error message to be far more productive to their users. In this post, I propose adding compile-time exceptions to C++ that will allow the developer to implement custom compiler errors for these cases.

For those unfamiliar with C++ templates: these allow you to do generic programming, meaning you can write functions without immediately specifying types, for instance a mathematical function that you want to be able to use with regular floats, double precision or even integers.

Implicit failure through implementation

Reading through Scott Meyers’ timeless classic Effective C++, I was struck by the following template in Item 45, especially the templated constructor:

This piece of code will allow you to initialize a SmartPtr by any “compatible type”. This for instance means derived types, as Meyers illustrates:

With regular pointers you can do:

The SmartPtr from the template above thus emulates the behavior of regular pointers, because thanks to the templated constructor you can do

The trick is that this will compile only if the implicit conversion from e.g. a Middle pointer to a Top pointer is allowed. This is, of course, the case, since a Middle pointer is a Top pointer (plus extra Middle stuff).

However, if the implicit conversion is not allowed, say if you try to initialize a child class object by a parent class one like this:

the compiler will complain as follows:

Welcome to C++ templates! What the hell am I seeing here? Bytecode?!

Looks perfectly normal to me…. (© wardyboy400, CC BY 2.0)

Debug messages should not be this ridiculously hard to read, should they? I honestly sometimes laugh hysterically at these kinds of errors, especially when they go on for many more lines, as they often do. It’s just too surreal.

Now, to be honest, once you succeeded in cutting through the clutter, this is a more or less clear message:

Or, to make it maybe a bit more understandable to an English speaker:

Compile-time exceptions: make intent explicit

The above “English” exception still does not convey one crucial fact: that the developer meant for this error to happen.

And for a specific reason: they want to emulate the behavior of regular pointers. A user of the SmartPtr should not be able to use this class in other ways.

But this fact is hidden from sight by the compiler messages that — while maybe helpful to developers and certainly very complete— are not at all helpful to end-users (not to mention the fact that the developer probably is an end-user themselves as well).

To help the end-user, a far better compiler error message would therefore be very similar to the one used for the normal pointers, for instance:

Or a bit more explicit:

If the compiler can detect these errors, which apparently it can, as evidenced by the error message, then why not allow the programmer to make use of this?

This would make the generic error message context specific, as in the above example: the developer adds their intention, the way the developer meant for the class to be used. This way, the user of the template does not have to browse documentation, nor parse huge template-error messages. Rather, they can fix the error and get back to work, as I must assume error messages were meant.

This line of thinking lead me to my eureka-moment: C++ should have compile-time exceptions! Consider the following (bold face) modification of our constructor:

This syntax would mean that if a specific exception occurs during compilation of the template — specifically in this case error E2064, a code which comes from this list of errors — the compiler enters the block, where it is met by some compiler specific syntax that allows for the developer to specifically express their intent.

Ok, so maybe we don’t need a special compile time output stream (it should rather be fmt based!), but you get the point. For brevity, I stream-print the types T and U directly, by which I mean their names should be printed.

This whole approach assumes the developer will know the pitfalls of their API and can provide such an improved error message. In the example given above, I think this is indeed a better message and I’m sure developers can come up with many other good examples of error messages that keep frustrating their users. Please let me know if you do, I’d love to make a follow-up post with the most interesting examples.

So, almost immediately after I came up with this, I was already enjoying (in my mind) the fame and riches that making this proposal would obviously bring me. I had never heard of this idea before, and indeed some Google searching (no further than page 1, of course, it’s still just a blog post…) did not lead to comparable ideas (I welcome your scorn, derison and ridicule if I missed something obvious).

However, while writing this post, I suddenly realized to my dismay that maybe we don’t need compile-time exceptions, at least for this use case.

We have Concepts now.

Why, Concepts, why? I thought we were friends…

Well defined failure: Concepts to the rescue?

In C++20, the same class behavior can be programmed with a Concept, which also makes the code’s intent more explicit. It makes the compilation fail at an earlier point: at the first line of the template, where the template argument is immediately tested for compatibility with the template class, instead of relying on the implementation (the incompatible pointer assignment) to fail compilation.

For instance, in our example, one could add a Derived concept, as found on the cppreference page on Concepts:

This concept can then be used to constrain the allowed template types of our constructor by replacing typename with our Concept:

Really nice, expressive syntax. I don’t want any old type U, I only want those that are derived from T.

The template now only compiles for types U that are derived from T (as it did before, but now because I say so, not because the language happens to). When you try to do otherwise, you get an error message that should be able to make this crystal clear for you! And all will be well.

The experimental Concepts-enabled clang on Compiler Explorer outputs this:

Wait a minute… this isn’t crystal clear at all. Concepts, why do you pain me so?!

Halt! Definitely something’s off here…

Granted, it gives a nice subtle reminder that I forgot to add a templated move constructor and that there’s also a non-templated implicit copy constructor (which, by the way, Scott Meyers also reminds you of in Item 45).

But honestly, this was not the feedback I was hoping for as a user. That feedback arrives only in the fourth note… of five!

I do like this sentence, especially that it starts with “because” (even though the “evaluated to false” is a bit awkward, but I’m nitpicking). But there is still too much noise in here, at least for an end user.

What about static_assert?

Lourens Veen reminded me that static_assert should not go unmentioned here. Indeed, this specific case can also be made more explicit using static_assert:

This approach has a number of its own issues though:

  1. It still gives us two error messages (see this implementation on Compiler Explorer): first the one about the pointer, which is triggered by the heldPtr initialization, and then the static_assert message.
  2. I don’t know how this could be combined with Concepts. If I understand correctly, neither Concepts nor the related Constraints have a body where a static_assert could go.
  3. We still cannot use it exactly the way we want, since the message of a static_assert must be a string literal, so we can’t dynamically display the types that are being passed as U and T.

More importantly, in general: not all errors can be represented by a boolean condition, which is necessary for the static_assert. Syntax errors for instance, by definition cannot be used, since we cannot define the correct syntax for them! If we could, they wouldn’t be syntax errors anymore…

The case for compile-time exceptions

I’m not by far a C++ guru, and also not a computer scientist, so it would take me a long time to come up with more examples and corner cases of what compile-time exceptions would mean. I’m hoping other C++ experts can chime in here.

Also, probably the name won’t stick. I guess what I’m describing here aren’t really exceptions (you can’t reasonably recover from them, for instance), but rather customized error messages. However, triggering them feels like catching an exception. Perhaps there is a different name for this hybrid thing.

Nevertheless, I do know that debugging C++, especially the templated kind, can be hell. There is just too much noise to wade through in the general case.

A library developer will often have a very good idea of the kind of things that are expected and that can go wrong. Why not use this knowledge to make life easier for library users?

Why can’t I just get a compile error that simply says “Error: argument to copy ctor for SmartPtr<T> can only be SmartPtr<U> when U is derived from T, but it’s not”? Or something like that.

For this to work, compiler errors would need to be properly classified, named, detectable with not too much effort, etcetera. I have no idea whether this is currently the case. So another question to you (especially if you’re a compiler expert): are compile errors defined in such a way (i.e. strict enough, in the language itself) that they could be implemented as specific exceptions? Could they even be defined like that?

Be sure to leave your comments below or on Reddit or Twitter!

Published in Netherlands eScience Center

We’re an independent foundation with 80+ passionate people working together in the Netherlands’ national centre for academic research software.

Written by Patrick Bos

eScientist / freelancer / HPC & data science / Physics & Humanities / C++ & Python

Responses (3)

What are your thoughts?