News

C++ Annotated: February 2021

Believe it or not, but it’s already the end of February. Time is flying by this year! This also means it’s time for our monthly C++ Annotated and its companion, the No Diagnostic Required show!

To bring you the best experience, we are delivering this digest in a few different forms. You can choose to read, listen, or watch our essential digest of this month’s C++ news.

  • Read the digest published monthly on our blog (use the form on the right to subscribe to the whole blog).
  • Subscribe to C++ Annotated emails by filling out this form.
  • Watch the No Diagnostic Required show on YouTube. To get notified of new episodes, follow us on Twitter.
  • Listen to our podcast just search for "No Diagnostic Required" in your favorite podcast player (see the list of supported players).

February news

Watch the new episode of No Diagnostic Required below, or just read on!

Contribute your insights about the C++ community – Developer Ecosystem 2021 survey

Before we get started with the news, we’d like to take a second to encourage you to fill out our annual Developer Ecosystem survey. Every year we run this extensive survey on the worldwide IT community. Later, we share the results and the raw data with the community for free. By taking the survey you have the chance to win an incredible prize and you can even sign up to receive personalized survey results to compare yourself with other developers. Help us gather insights about trends in the C++ community.

Language news

As well as the usual monthly committee mailing of new and updated papers, we’ve just had another virtual plenary session.

This is where proposals that have progressed to a point that the various subgroups are happy with them are formally proposed to the whole committee and voted on for adoption into the working draft of the next standard. We missed the first few of these after moving the meetings online because of the pandemic, but they began again in a new virtual format toward the end of last year, and this week the second one was held.

Herb Sutter has a great (as usual) "trip report" covering the highlights from the session. We’re still being very conservative and only adopting small tweaks and bug fixes – and it’s likely we won’t approve any of the big features we’ve been hoping for from C++23 until the meetings resume face-to-face, which will hopefully be next February, according to the current plan.

One of the tweaks adopted into the draft that Herb highlighted was P1102. This allows you to omit empty ()s on a lambda declaration, even if the mutable keyword is there. We’ve always been able to omit empty lambda parameter lists in the non-mutable case, so this just makes things more convenient and more consistent. By eliminating the need to have to remember when you can and can’t omit the (), Herb hopes more people will do so.

[x]{ return f(x); } // Legal C++11
[x] mutable { return f(x++); } // Illegal until C++23

The theme of making existing things work more consistently and as expected runs through most of the adopted proposals, which has to be a good thing, even if some of us are starting to get a little impatient with waiting for some of the bigger feature proposals!

Speaking of which, some of those bigger features are still being discussed, as reflected in the February mailing. For example, there were a couple of papers (P2093R4 and P2216R3) that build on the new std::format – a minimal, initial version of which made it into C++20. P2216, in particular, aims to add compile-time format string checks – arguably one of the coolest features of Victor Zverovich’s reference library, {fmt}. For example (from the paper):


    std::string s = std::format("{:d}", "I am not a number");

In C++20 this results in a runtime exception (format_error), whereas under the proposal, this would fail at compile time.

Another highlight from the mailing is not a proposal at all, but a rather far-reaching view of the ongoing role of C++, from Bjarne Stroustrup: "C++ – an Invisible Foundation of Everything". In just 9 pages, Bjarne gives a potted history of the evolution of C++, in the context of what it’s for, what its core principles are, how they are being met (or otherwise) today, and where he sees it going in the future. Along the way, he attempts to convince the reader that C++ is still the best tool for a substantial number of exciting applications – from search engines and browsers to cars and Mars Rovers!

More concepts

The More concepts library is intended to help categorize the STL containers and write some generic code that works with a selected category. For example, you aim to write code that accepts any container that allows mutable iteration, a container that knows its size, or a sequence container that allows efficient inserting/erasure at the front.

The library brings several groups of such concepts:

  1. Base containers concepts (“can be cleared”, “allows reverse iteration”, “knows its size”, “allows mutable iteration”).
  2. Sequence container concepts (a double-ended sequence container that provides indexed access to elements or a random-access sequence container that is stored contiguously in memory).
  3. Associative container concepts (associative containers with various unique and non-unique keys constraints, like a map container with unique keys, for example).

There are also some wrappers over standard type traits and a mock_iterator class template to create your own specific concepts.

build2 with C++20 modules support

build2 is an open-source build toolchain, which consists of a build system, package manager, and project manager. And it now supports C++20 modules (when used with GCC). To get the most flexible solution, build2 simply discovers module and header unit dependencies and communicates their mapping to the compiler.

And there is a hidden gem inside. Boris Kolpackov, an author of build2, collected a library of basic C++20 module samples to illustrate how they can be used with build2: from a minimal example of a names module consisting of just 3 files, to header units and automatic on-the-fly translation of #include directives to import declarations.

Clang Power Tools

Clang Power Tools was expanded with the new Clang-Format Editor tool and became free for everyone, including commercial use. The instrument originally brought Clang-Tidy and Clang Analyzer to Visual Studio C++ users. Clang-Format Editor, on the contrary, is a standalone tool that can be used independently of any IDE you choose. One of the most interesting abilities it has is a style detector that can extract a Clang-Format configuration out of the given source code and save it to a .clang-format file. It visualizes the extracted set of options and shows the code differences while you are updating the option values. This might be quite useful, for example, if you’d like to keep the code base indentation and other formatting rules while editing it but you have no idea which exact style was used originally.

Postfix completion for C and C++ in JetBrains tools

Coding is a truly creative process that starts with an idea and then coding it in a proper way, supported by your language of choice. No IDE can help you come up with the idea, but some IDEs definitely can assist with the language constructs that come next (and even before!).

Postfix completion lets you add code around an expression you’ve just typed. It can wrap an expression with a frequently used language construct, or pass the expression as the first argument of a free function. We’ve had it in the ReSharper C++ extension for Visual Studio for a while already, and now it is available in CLion, our cross-platform C/C++ IDE. You can type .not to negate the already typed expression, wrap with a C++-style cast, or begin/end idioms. Many predefined templates for C and C++ are available. But you can also type an expression first, and later pass it to a free function by simply typing a dot and a function name or by selecting a free function from the completion list.

C/C++ search extension

The news actually came on the last day of January, but was only noticed in February. Users of Chrome, Firefox, and Edge can search cppreference, one of the most popular and useful Wiki for C++ developers, right from the address bar. They just need this handy C/C++ search extension. It’s as easy as typing cc and then a search request. And you can even search by library header; for example, type cc :header chrono to read through the header which is part of the date and time library.

This was released in 2020 and recently got support for multi-locale language cppreference docs search and search through POSIX system interfaces.

And finally, when can your compiler devirtualize a call?

You’ll often hear talk of devirtualization but, like most compiler optimizations, it’s hard to know when they might apply or what might defeat them. If you’re performance-sensitive enough that the overhead matters, you’re probably better off avoiding them for something like a type-erasure approach (but always measure!). But if you’re more curious, Arthur O’Dwyer has put together an insightful post, "When can the C++ compiler devirtualize a call?". It explicitly side-steps LTO (Link Time Optimization), which should be more effective thanks to whole-program analysis but, nonetheless, is a fascinating deep-dive on the sorts of things your compiler can do for you, even with essentially dynamic features.

image description