C++ Annotated: June – September 2018
Hi,
A new edition of C++ annotated brings you a collection of the most important and engaging news from over the last 4 months that you should definitely check out. In this edition:
- Hot C++ conference season has just started with CppCon
- New section! Proposals you can’t miss
- Learning
- Recent releases
CppCon 2018
The summer months were marked by only two C++ events: C++ committee meeting in Rapperswil, Switzerland, and ++It – a small but aspiring C++ conference in Milan, Italy. In September, however, a hot C++ conference season kicked off again with CppCon. Pacific++, C++ CoreHard, Meeting C++, ADC, code::dive, and the C++ Committee meeting in San Diego, California, are coming up later this year.
This year’s installment of CppCon was called the best ever by many attendees (though not all). We fully agree! The Keynotes this year didn’t unveil any new surprises but summarized the huge and important work going on in the C++ world. Bjarne Stroustrup talked about the importance of Concepts, while Herb Sutter analyzed the language evolution and discussed the metaclasses proposal update and new lifetime checks. Chandler Carruth ran a mind-blowing keynotes session on Spectre and the famous meltdown issues, followed by a dedicated panel.
Regular sessions covered a whole variety of C++ topics: initialization, compile-time programming in C++20, and compile time regular expressions (this talk by Hana Dusíková is said to be the best one this year by many attendees!), discussions on modules, lifetime, new static exceptions, memory tagging technique, and tooling. There were also some really useful general learning sessions, such as Best practices in C++ by Jason Turner. Quite a few sessions are already available on YouTube – now you just need to find some time to watch them! There are several trip reports worth mentioning:
- CppCon 2018 trip report, by Mathieu Ropert
- CppCon 2018, by Rainer Grimm
- Report on CPPCon 2018, by Eduardo Madrid
- CppCon 2018 Trip Report, by Matthew Butler
- JetBrains C++ team at CppCon 2018: trip report
In addition, you may want to watch the Channel9 interview (recorded right at CppCon) with Bjarne Stroustrup and Gabriel Dos Reis talking about Concepts. “Your compiler doesn’t know what you are trying to do; it only knows what you did,” says Bjarne in the interview, and this is why concepts are specially created to explain your intentions to the compiler in the world of metaprogramming.
Proposals you can’t miss
Today we are starting a new section in our regular C++ Annotated. In each issue, we will cover a selection of C++ proposals and initiatives you definitely should learn about while developing in C++. This time we unveil static exceptions, constexpr new, and lifetime checks. See details below.
Static exceptions
p0709r1
Thought exceptions weren’t for you? Do you have hard real-time requirements, need absolute determinism, and are concerned about performance, or do you just think that exceptions are only for “exceptional” situations?
This proposal changes all that and gives you a combination of all the best parts of exceptions and return codes, without the corresponding trade-offs. Think that’s too good to be true? Read the proposal – or watch one of Phil’s talks (at C++ Now 2018 and at CppCon 2018).
Constexpr new
p0784r4
The proposal is actually (currently) “More constexpr containers” (formerly “Standard containers and constexpr”). Right from the introduction it cites, as motivation, Ben Deane and Jason Turner’s classic talk, “constexpr All The Things”. A large part of the proposal is dedicated to dynamic allocation within a constexpr context – which also requires addressing constexpr destructors. If this is already blowing your mind, wait until you hear about transient vs non-transient allocation! If it’s not, then either you’re already familiar with the proposal, or you’ve not done enough constexpr or template-metaprogramming to get scared yet! There are a number of concerns that need to be addressed. The proposal picks them off, one by one, and proposes approaches which sound sensible for each.
Lifetime
This is not actually an ISO proposal, but rather a refinement of one of the big promises from the Core Guidelines, which Bjarne Stroustrup first unveiled at CppCon 2015. Then it was followed up at the same conference by Herb Sutter, who dug deeper into the lifetime parts. The promise, from Herb’s follow-up, was that, if the Core Guidelines (or at least the parts relating to object lifetimes) were implemented by compilers and/or static analyzers to a sufficient degree, lifetime and ownership issues could be a thing of the past – and possibly many uses of smart pointer would be unnecessary! We know that Rust has achieved the same sort of thing, although the concern there is that the syntax is very annotation-heavy. The CG approach requires much less in the way of annotations.
At CppCon 2018, Herb gave us an update on this. The original dream may be too ambitious to do in full for now, but by limiting the scope to catching common cases we can still eliminate many of the worst types of lifetime issues, such as dangling references, that plague us today.
Learning
Constructing C++ objects without copies
This guest blog post at Fluent C++ by Miguel Raggi is all about minimizing copies when constructing an object that holds a value. And while copying from lvalue/moving from rvalue case is simple, referencing an lvalue/moving from an rvalue is actually not. The article goes through the solution step by step, discusses the use of forwarding references, and suggests a way of adding const to the stored reference when lvalue is provided as input.
Std::shared_ptr via make_shared or constructors?
This article by Arne Mertz raises the question, “How is it better to construct std::shared_ptr
?” Obvious candidates are std::make_shared
and std::shared_ptr
constructors. The author provides a nice visual representation of the memory allocation happening in both cases to explain the difference. While one might say that a single allocation is always better, there are pros and cons for std::make_shared
. For example, when talking about the drawbacks of a single allocation solution, it has to be granted proper access rights and might lead to a considerable amount of memory locked in specific cases.
Everything You Need to Know About std::variant from C++17
Bartlomiej Filipek runs a very useful series about C++17 Library Utilities in his blog. If you want to learn about std::optional
, std::any
, std::string_view
, or std::filesystem
, his blog is the right place to do it. You will find detailed descriptions and samples, as well as an overview of cases where the feature is useful and required. The blog post we’d like to mention here is dedicated to std::variant
, a type safer union, especially when complex types are used inside. The main benefit is that the object lifetime which std::variant
cares about automatically calls the destructor when needed. From the article, you can learn how to create std::variant
and what std::monostate
is, how to change the value in std::variant
, and how to use the corresponding helpers and visitors. Finally, you’ll learn about memory consumption (no dynamic allocations are used!) and go through several real-world use cases.
Passing overload sets to functions
Andrzej Krzemieński in his blog raises an interesting problem about passing a function name as a function argument, which actually names an overload set and not a particular function. Now, take into consideration that the C++ standard is allowed to change function signatures and overloads in the standard library in the backward compatible way. This means we’ve come to a situation where code compiles with one standard library and fails with another one, or the code might stop compiling with the standard library evolution in time. Andrzej discusses the possible solutions, including passing in a function object or using lambdas. Some trickier macros and even a language proposal are also covered in the article.
Useful non-default GCC address sanitizer checks
Google Sanitizers are very handy nowadays for catching particular groups of errors, including memory issues. In this article, Krister Walfridsson lists some useful GCC address sanitizer checks that are not on by default, but you may want to consider using them. Use after return, comparing pointers from different objects, subtracting pointers to different objects and out-of-bounds accesses to the unused capacity of a std::vector
are covered. By the way, in CLion we have the first check enabled in CLion’s default configuration, because we consider it super useful for many dangerous error cases.
C++ Committee meeting in Rapperswil
In June, the C++ Committee held a meeting in Rapperswil, Switzerland, which was very important as it was one of the last meetings on the design of major C++20 features. C++20 is expected to be feature-complete by the beginning of 2019. And while some topics are still triggering hot discussions in the committee (for example, talking about Modules), if you check out the summary from Bryce Lelbach on reddit, the majority of the work is mostly done now. In Rapperswil, contracts, standard library concepts, feature test macros, NTTP, and other features were added to the C++20 draft. Our JetBrains C++ team was also happy to join the meeting, as you can find out from our report.
Life as a C++ Dev
This unusual post on reddit is a great opportunity to find out what the typical conditions the C++ developers around us are working in. Pre- or post- C++11 code, legacy code base or freshly started projects, custom STL library, C with classes, a story about throwing away a 900-line template – check out how the C++ world looks when it comes to real developer stories. And maybe share yours in the comments!
Releases
Qt Creator
Qt Creator 4.7 was released in July. In this version, the Clang code model was finally turned on by default, improving the accuracy of code analysis, highlighting, completion, and renaming (for local symbols). Some other changes include the Run Test Under Cursor action and improvements on Windows for MSVC.
Things are moving quickly forward and Qt Creator 4.8 Beta is already announced, promising a client for the language server protocol to Qt Creator. The client is capable of performing code completion, highlighting of the symbol under cursor, and jumping to the symbol definition. LSP integration is getting more and more attention nowadays from IDE vendors. For example, Apple recently announced a new LSP language service supporting Swift and C-family languages.
Sourcetrail 2018.3
A cross-platform source code explorer and visualizer, Sourcetrail had another big update in August. It now allows code browsing while indexing the project, which improves the performance aspect a lot as C++ projects can take quite a long time to index. Two new supported source group types include Code::Blocks and Sonargraph. Besides this, the graph view was also improved, as well as the code view getting some useful enhancements, like better local references navigation and automatic horizontal scrolling to the source code position.
Visual Studio 2017 version 15.8
August was also the month to release the new Visual Studio. Since this version, C++ developers can benefit from a notable debugger feature – Just My Code enables them to step-over code from system or third-party C++ libraries in addition to collapsing those calls in the call-stack window. Regarding the work with macros, the C++ Quick Info tooltip shows the macro expansion, and you can also now convert a macro to constexpr. Besides these additions, IntelliSense for templates makes it possible to use code completion inside the template body, which is based on the provided information about the type used to instantiate the template.
CLion 2018.2
CLion is definitely becoming more mature and growing into a strong member of the C++ family. The new release at the end of July brought with it a Clangd-based language engine as a complementary engine to CLion’s own language engine, for showing code errors and warnings in the editor. On the one hand, it’s a first step in experimenting with how many language-related features we’ll be able to implement on top of clangd. On the other, it’s a great addition to CLion’s functionality, enabling accurate code analysis for many tricky C++ cases, especially those where the latest C++ standards are in use.
CLion has also added support for two new project models: compilation database and Gradle for C++. The first one actually means you can now work with Makefiles projects in CLion, using a workaround described in our blog. Google Sanitizers support, the Database Tools and SQL plugin, and other massive improvements made this release a big thing!
If that’s not enough, check out the Early Access Program for the upcoming 2018.3 release, which already gives you the opportunity to evaluate remote development support in CLion and the CPU profilers integrations.
ReSharper C++ 2018.2
ReSharper C++, a Visual Studio extension for C++ developers, also had an important update. One of its key improvements, however, is mostly interesting to those who work with both C++ and C# code, and use ReSharper Ultimate. From now on, ReSharper C++ will provide initial support for C++/CLI, a set of C++ language extensions by Microsoft designed for easy interoperability between managed and native code. Code analysis, typing assistance, formatting, and code completion were all updated to support C++/CLI. Navigation actions and will work across both – C# and C++/CLI.
Version 2018.2 also added support for a huge set of C++17 features and even some from C++20, like CTAD, fold expressions, coroutines, designated initialization, and more. And finally, ReSharper C++ now provides spell-checking functionality out of the box!
ReSharper 2018.3 Early Access Program has been opened already, bringing you predefined naming schemes for common C++ code standards and huge performance improvements for Unreal Engine 4 projects.