C++ Annotated September–October 2022: Kona ISO Meeting Results, Pattern Matching, New Integers, and Tools Updates
We’re back, bringing you all of the news from September and October 2022 in our latest C++ Annotated digest:
- Language news
- Conferences
- Learning
- Tooling
ISO C++ Committee meeting Kona 2022 summary
From November 7-12, 2022, the ISO C++ committee met in Kona, Hawaii. This was the first in-person committee meeting in almost three years, and the penultimate meeting for C++23. At this stage, C++23 is past feature freeze, and the committee is ironing out some last-minute issues.
In one of the most impactful changes voted into C++23 last week, the committee finally fixed a long-standing issue with range-based for
loops (P2644). When a temporary object is created in the for-range-initializer of a range-based for
loop, its lifetime will now be extended, removing the undefined behavior:
for (char c : getData().getValue()) { … } // UB in C++20, OK in C++23
A few standard library features made it into C++23 at the last minute, such as monadic functions for std::expected
(P2505), while others such as std::function_ref
(P0792) will now definitely have to wait until C++26. The committee also discussed removing [[assume]]
(P1774) from C++23 again (after it was just voted into C++23 during the last plenary). Some people were worried about adding another inherently “unsafe” feature that can cause undefined behavior and is very hard to use correctly, and asked the committee to reconsider. In the end, there was consensus to leave the feature in, despite it being for experts only, due to the notable performance improvements in certain cases.
Meanwhile, the language evolution (EWG) and library evolution (LEWG) working groups are already working on new features targeting C++26. EWG considered two competing proposals for pattern matching: Michael Park’s proposal offering a composable approach and Herb Sutter’s pattern matching using is
and as
, which is more chaining than composing. EWG prefers the former as a solution for pattern matching. LEWG voted to forward RCU (P2545) and hazard pointers (P2530) to a wording review targeting C++26 and considered many other proposals at an earlier design stage. However, some highly anticipated features, notably reflection, did not make any progress at this meeting. SG7, the reflection study group, appears to be dormant at the moment.
Work is also continuing in the other study groups. SG15, the tooling study group, is working on the idea of having a new international standard for C++ tooling. The goal is to help different parts of the ecosystem such as compilers, build systems, package managers, and others talk to each other more easily by standardizing interfaces between them. This effort builds on prior work like the JSON-based format for describing C++ source file dependencies (P1689), and there is more happening in this space. However, it is not completely clear what else might be in scope for this potential new standard.
SG21, the Contracts study group, voted in favor of a roadmap to get Contracts into C++26 (P2695). There are still a few things about Contracts that SG21 has not figured out yet (P2521), in particular how to deal with side effects in contract annotations and what syntax to use (attribute-like or lambda-like), but it looks much more likely that there will be progress on this feature, which is good news. Meaningfully annotating C++ code with preconditions, postconditions, and assertions will be very useful for finding bugs across API boundaries and giving code analyzers more information about our code.
The next committee meeting will take place in February 2023 in Issaquah. This will be the last meeting dedicated to resolving C++23 issues. At Issaquah, technical work on C++23 will be completed.
NDC TechTown, CoreC++, CppCon
Timur Doumler visited 3 big C++ events in 2 weeks and wrote a trip report about them.
NDC TechTown was held in Kongsberg, Norway, from August 31 to September 1, 2022. It was opened by Kate Gregory with a very useful talk titled Abstraction Patterns. I (Anastasia Kazakova) was also at the event as a speaker and attendee, and my favorite talk was A preview of C++23 by Deniela Engert, which you can already find published in the conference playlist.
Core C++ was held in Tel Aviv, Israel, on September 6–7, 2022. Lots of talk recordings from there are already available in the conference’s playlist. This includes Inbal Levi’s talk, Customization Methods, Jon Ross-Perkins’s overview of the new Carbon language from Google, and some others that Timur mentioned in his trip report.
Finally, CppCon was back to Gaylord Rockies Resort, Denver, Colorado, on September 12–16, 2022. There were quite a few talks on C++23, including Timur’s keynote, How C++23 Changes the Way We Write Code. It’s also worth mentioning two new C++ successors presented at the conference: Val by Dave Abrahams and Cpp2 by Herb Sutter. Check out the playlist from the conference, which still has new videos coming to it.
is void
by Herb Sutter
Herb Sutter moves forward with this proposal on pattern matching in C++. Two foundations of this proposal are is
(a general type or value query) and as
(a general cast, for only the safe casts). The idea was presented at CppCon 2021 and is supported by the corresponding language proposals and the test implementation in the Cppfront compiler.
In his new blog post, Herb discusses the testing for “empty” ability. He talks about various spellings for “empty state” and how they are unified. Given that the language itself has had the concept of “empty” type since the beginning – void
, the author implements is void
as a generic “empty state” test. With this proof of concept working successfully in the Cppfront compiler, Herb presents the updated version of the ISO C++ proposal paper.
5 Ways to Improve or Avoid Loops in C++20…23
While loops are an essential language construct, C++20 introduced many alternatives to them. If you need to iterate over a container, you can also use iterators, algorithms, or ranges/views. In his new article, Bartlomiej Filipek discusses these approaches, along with some of the pitfalls of each.
The article first discusses a few fixes to the loop-based solution to avoid signed/unsigned mismatch issues. In the solution with iterators, the integral types are removed completely. In C++20, however, we have ranges that abstract the iterators and give us nicer syntax, which is why the author offers a range-based solution, as well. C++ also provides many views (and more are coming with C++23), and Bartek’s third algorithm using the pipe operator and the reverse
view is short and expressive. His fourth solution is based on the zip
view, introduced in C++23. Finally, one of the article’s readers suggests a lambda-based solution to round out the picture. Check them out, and feel free to explore the code samples using the included links to the Compiler Explorer.
Working with a PEmicro Multilink Debug Probe
If you work with ARM-based microcontrollers, you may have heard about the PEmicro Multilink debug probe. Multilink debug probes allow you access Background Debug Mode (BDM) or the JTAG interface on a wide range of ARM Cortex devices. With this probe, you can read/write registers and memory values, debug code on the processor, and program internal or external FLASH memory devices.
Timur Doumler, JetBrains C++ Developer Advocate, tried using it with a BluePill with a STM32F103 ARM Cortex M3 microcontroller to write a basic “embedded” “Hello, World” program, which makes an LED blink. Timur logged all his steps, from installing the software and connecting the hardware to configuring and creating the actual code. Later, he recorded a video and wrote a short blog post with useful links for those who would like to try their hand at using the debug probe.
What errors PVS-Studio found in LLVM 15.0
The PVS-Studio team has analyzed many known projects, including popular compilers. Recently they posted about the issues they found in LLVM 15.0. It’s interesting to note that the code analysis part of the LLVM codebase had already been checked with the Clang static analyzer and Clang-Tidy (members of the LLVM family), and Coverity (a separate analyzer).
Some of the most interesting errors reported by the analyzer are potential null dereferencing and ‘array out of bounds’ issues. These are dangerous bugs that can do a lot of damage at runtime. The analyzer also detected various redundant code, which, while not strictly speaking an error, might still indicate flaws in logic or an imprecision that could potentially lead to incorrect compiler behavior.
New integer type ideas
In his new blog post, Jonathan Müller talks about several new integer types he would like to see in C++. The first is symmetric signed integers, where the minimum negative value is named not-a-number (INT_NAN
). By equalizing the number of negative and positive values of the signed type, this would make many currently non-symmetric operations of such type symmetric and thus prevent them from overflowing. It also would allow std::optional<int>
to fit int
. Instead of having to store an additional boolean to keep track of the existence of an optional, we could just store INT_NAN
instead.
The second type proposed in the article is unsigned integers with one bit missing. For example, a 63-bit unsigned integer would be represented the same way a 64-bit signed integer would. However, if it ever stored a negative value, that would be considered undefined behavior. Technically, it’s simply a signed integer type with assertions to check non-negativity, only better because it is built into the type system and implemented by the compiler.
The author’s third idea is to make integer types and bit integer types different from each other in terms of available operations, so the bit operations would only be supported for the bit integer types.
Which of these integer types would you like to see in C++?
Copy-on-write with Deducing this
Barry Revzin, one of the co-authors of deducing this
, wrote a blog post on yet another use case for the feature: implementing a copy-on-write strategy when the actual copy is postponed until you really need it. A Vector<T>
implementation is used as a playground as the author writes the copy-on-write version of it. In the first discussed implementation, the structure stores the state and the ref counter, and the mutable and const accessors behave differently. With the deducing this
feature, operators become a template.
Another solution includes two structures – ImmutableState
and MutableState
– and enforces the proper parameter type via deducing this
. Quite an elegant solution! But Dave Abrahams disagrees and calls the implementation pessimistic. What do you think about this? Have you met interesting real-life use cases for this new C++ feature? Feel free to voice off in the comments below!
A new book about learning modern C++
Learning C++ is a new book coming from Manning Publications about C++, a step-by-step journey to C++ focusing on the modern C++20 standard. The authors Michael and Ruth Haephrati take a practical approach, dedicating significant time to the practical aspects of using the language features.
The book should be a great learning resource for those new to C++, as it covers important and valuable C++ basics like language constructs, operators, working with string literals, introduction to pointers, and STL. In the second half, the authors explore ranges, concepts, multithreading, modules, and exception handling – all new and hot topics introduced to the language in recent years. These later chapters are not yet available but coming soon.
Thanks to Manning Publications, our readers can get this book with a 35% discount. Use the learnc35 discount code.
As a side note, we’d like to remind you that at JetBrains, we welcome all C++ teachers to create their C++ courses with the help of our free Educational Tools for CLion.
Qt Creator 9 with CMake Presets
CMake Presets is a way to maintain and share the CMake configuration of your project inside your team. Qt Creator 9 is the first version of Qt Creator that supports CMake presets. Qt Creator’s blog describes its current implementation and some known limitations.
Qt Creator supports presets up to version 3, including support for configure presets, build presets, and conditions. Test presets are now yet supported.
CMake presets support is implemented on top of Qt Creator’s Build import feature. Because of that, you can configure presets only during the very first time you configure a CMake project. If you want to reload the presets, you’ll have to remove the build directory and the CMakeLists.txt.user file.
The future of C++ compiler diagnostics in MSVC and Visual Studio
Microsoft announced improved compiler diagnostics and enhanced Visual Studio viewers. With the new language features like concepts and ranges, the compiler can do a better job of explaining the errors it encounters. For example, it can explain the constraints failures in the code using these features.
The current state of error messaging in the main C++ compilers is presented in a paper to WG21 written by Sy Brand, Microsoft Developer Advocate, which suggests potential improvements. What’s more, Visual Studio 2022 versions 17.3 and 17.4 have already implemented some of the newly suggested principles.
The new Visual Studio versions come with more friendly reporting of unsatisfied constraints in the compiler and a new in-box viewer for such errors. For example, the compiler now lists all candidates in the overload resolution, including the template, and explains why each candidate fails. For concepts, the compiler provides explanations for all associated constraints that contribute to a given failure.
Vcpkg celebrates 6 years with over 2,000 libraries
Vcpkg, the cross-platform open-source package manager, celebrates its 6-year anniversary and shares some impressive statistics about its progress:
- Over 2K unique open-source libraries are available in the catalog.
- Over 10K unique library versions.
- Supports architectures like x64, x86, arm, arm64, and WebAssembly.
- Supports platforms such as Linux, macOS, Windows, iOS, Android, FreeBSD, and UWP.
- Supports MSVC, Clang, GCC, and Mingw, among other compilers.
- The main vcpkg repo has 4.8K forks on GitHub.
Looks impressive for 6 years of work, doesn’t it? You can find more vcpkg statistics, along with the September 2022 release notes, in the Microsoft blog.
CLion 2022.3 EAP with C++20 Modules and the CMake Debugger
The Early Access Program for CLion 2022.3 has been running since mid-September, letting anyone interested preview C++20 Modules and CMake Debugger – the two major enhancements that we plan to release in November.
Modules are a new feature in C++20. They were introduced to reduce many of the problems associated with the use of header files, by isolating declarations and keeping their visibility limited to the module. There is also a hope that modules will be able to reduce compilation times in many cases.
The feature had a long and challenging path from the initial idea to its acceptance as part of the standard. Even as part of the standard, it still suffers from the lack of tooling support. Microsoft implemented some support for C++20 Modules in Visual Studio and in their compiler. They even made it work for CMake with the Visual Studio generator. CLion now also supports C++20 modules regardless of the approach you take to compile them in your project. Support for C++20 modules is also going to be available in ReSharper C++ 2022.3. Once the most popular C++ tools are finally capable of working with this feature, that might usher in some big changes in the way we write C++ code!
Another interesting improvement in CLion 2022.3 EAP is the CMake Debugger. CMake is a tricky scripting language that is sometimes hard to work with. Since it is a language of a build system, identifying the best way to debug an error or a suspicious behavior in a CMake script is not always easy. To lend a helping hand, CLion now comes with a built-in CMake Debugger, which allows you to set breakpoints, step through the code, and watch CMake variables, targets, and environment variables in the debugger tool window.