Once More About the Rule of 5 -- Sandor Dargo

SANDOR_DARGO_ROUND.JPGIn a recent talk at C++OnSea, Arne Mertz highlighted common misuses of guidelines, including the Rule of Five. This discussion prompted me to reflect on a recurring pattern I've observed in C++ classes that explicitly default constructors and destructors, leading to unexpected behaviors with move semantics.

Once More About the Rule of 5

by Sandor Dargo

From the article:

Let’s first repeat what the rule of 5 says.

The Rule of Five tells us that if we need to define any of a copy constructor, copy assignment operator, move constructor, move assignment operator or destructor then we usually need to define all five.

Fair enough.

Have you ever seen classes where the default constructor and destructor are explicitly defaulted? Like this?

class SomeClass {
public:
    SomeClass() = default;
    ~SomeClass() = default;

    void foo();
private:
    int m_num{42};
};

First of all, that’s not the best idea. You can simply remove them. But let’s assume that you cannot remove the user-provided destructor for some reason. Maybe it’s not defaulted and it does something.

What does the famous Hinnant table tell us?

Reminder: CppCon 2024 Regular registration ends Friday, Late registration opens Saturday

The opening keynote of CppCon 2024 is just 20 days away! The program has over 100 great talks and panels, and the five keynotes have been announced:

If you're interested in savings, the Regular price for on-line and on-site tickets is available until this Friday, August 30. After that tickets will still be available right up to the conference, but at the late registration ticket price.

To register for CppCon 2024 at the Regular rate, click this link this week.

For details of on-line and on-site tickets, see the Registration page which includes information about student registration discounts, group rates, the CppCon Academy (extra pre- and post-conference classes by world-renowned instructors including online classes for those who can't be there in person), the community dinner, the "Meet the Presenters" banquet, and much more!

CppCon 2024 Back to Basics: Functional Programming in C++ -- Jonathan Müller

Registration is now open for CppCon 2024! The conference starts on September 15 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting some upcoming talks that you will be able to attend this year. Here’s another CppCon future talk we hope you will enjoy – and register today for CppCon 2024!

Back to Basics: Functional Programming in C++

Thursday, September 19 09:00 - 10:00 MDT

by Jonathan Müller

Summary of the talk:

Functional programming is a declarative way of writing programs by composing functions.
In many situations, this can lead to code that is easier to write and understand and less error-prone.
However, it requires a shift to a more functional mindset.
This talk gives an introduction to functional programming in C++ using the modern standard library.
We will cover algorithms using `std::ranges`, composable error handling with `std::optional` and `std::expected`, algebraic data types, and separating IO from computation.
In the end, we'll even cover the M-word.


Jonathan is a Software Engineer at think-cell. There, he is responsible for maintaining think-cell's core libraries, which include a custom range library, a fast and convenient JSON parser, and many other utilities and data structures to write elegant C++ code. Before working at think-cell, he wrote many useful open-source C++ libraries. He is also a member of the C++ standardization committee, where he serves as the assistant chair for std::ranges, and a frequent conference speaker.

C++ programmer's guide to undefined behavior: part 4 of 11

Your attention is invited to the fourth part of an e-book on undefined behavior. This is not a textbook, as it's intended for those who are already familiar with C++ programming. It's a kind of C++ programmer's guide to undefined behavior and to its most secret and exotic corners. The book was written by Dmitry Sviridkin and edited by Andrey Karpov.

C++ programmer's guide to undefined behavior: part 4 of 11

by Dmitry Sviridkin

From the article:

In the C++98, the committee made a terrible decision that seemed reasonable at the time. They created a specialization for std::vector<bool>. Normally, sizeof(bool) == sizeof(char), but one bit is enough for bool. However, 99.99% of all possible platforms can't address memory one bit at a time. Let's pack bits in vector<bool> and store CHAR_BIT (usually 8) boolean values in one byte (char) for more efficient memory utilization. As a result, one needs to work with std::vector<bool> in a very special way...

CppCon 2024 Building Safe and Reliable Surgical Robotics with C++ -- Milad Khaledyan

Registration is now open for CppCon 2024! The conference starts on September 15 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting some upcoming talks that you will be able to attend this year. Here’s another CppCon future talk we hope you will enjoy – and register today for CppCon 2024!

Building Safe and Reliable Surgical Robotics with C++

Wednesday, September 18 15:15 - 16:15 MDT

by Milad Khaledyan

Summary of the talk:

This talk examines the use of C++ in building distributed robotic surgical systems, emphasizing safety, performance, and reliability. While C++ offers strong performance benefits, it also presents challenges in meeting industry standards and regulations in the medical technology field. We discuss the architectural decisions and strategies employed to meet international safety standards for medical devices, and present techniques for writing efficient, safe and reliable software. Our experience in building a surgical robotic system serves as a case study, highlighting the challenges and solutions in this highly regulated domain.


Milad Khaledyan is a Staff Robotics Software Engineer at Johnson & Johnson MedTech, based in Santa Clara, CA, USA. With extensive experience across various companies, he specializes in software development for robotics in medical devices, as well as autonomous robots in manufacturing settings. Milad earned his doctoral degree in Mechanical Engineering, specializing in Robotics and Control, from Louisiana State University in 2018.

 

What's so hard about constexpr allocation? -- Barry Revzin

Before C++20, constant evaluation couldn't handle allocations, causing any such attempts to fail. This changed with C++20, which introduced the ability to allocate memory during constant evaluation, although with strict limitations requiring deallocation during the same evaluation period. Despite these advancements, certain operations, such as declaring a constexpr std::vector, remain impossible, with the goal of this blog post being to explore why these limitations persist.

What's so hard about constexpr allocation?

by Barry Revzin

From the article:

Before C++20, we couldn’t have any allocation during constant evaluation at all. Any attempt to do so would fail the evaluation — it would no longer be constant.

In C++20, as a result of P0784R7, that changed. Finally we could do allocation during constant evaluation. However, this evaluation was extremely limited. Specifically, any allocation that happens must be deallocated during that constant evaluation.

This opened the door to a wide range of operations that weren’t possible before. We can now have local std::strings and std::vector<T>s! Those just work!

But we still cannot just declare a constexpr std::vector:

#include <vector> 
constexpr std::vector<int> v = {1, 2, 3}; // error 

And we cannot even declare a local constexpr std::vector in a consteval function:

#include <vector> 
consteval auto f() -> int { constexpr std::vector<int> v = {4, 5, 6}; // still error 
return v.size(); 
} 

This limitation still remains in C++23 and could very well still remain in C++26. The goal of this blog post is to explain why we haven’t just solved this problem already.

CppCon 2024 Leveraging C++20/23 Features for Low Level Ineractions -- Jeffrey Erickson

Registration is now open for CppCon 2024! The conference starts on September 15 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting some upcoming talks that you will be able to attend this year. Here’s another CppCon future talk we hope you will enjoy – and register today for CppCon 2024!

Leveraging C++20/23 Features for Low Level Ineractions

Tuesday, September 17 14:00 - 15:00 MDT

by Jeffrey Erickson

Summary of the talk:

Low level interactions are a core part of embedded implementations. All too often, C++ developers rely on C constructs and interactions due to prior biases around language support.  Herein we present effective leveraging of C++20 and C++23 constructs in an embedded driver code base. From using an existing C driver more effectively with modern C++ smart pointers to leveraging constexprs for bit and byte manipulation in the standard library, we will go over how you can stay on the cutting edge of the C++ language evolution in the embedded space.

 

Jeffrey E Erickson works in HW/SW Codesign Architecture at Altera, an Intel company. He holds a BS in Electrical and Computer Engineering from the University of Virginia and a doctorate from Rutgers University and UMDNJ. For 15 years he has worked in embedded systems development including FPGA-processor integration, secure firmware development, and systems modeling. He holds 3 patents in image processing and signal integrity.

 

CppCon 2024 Peering Forward - C++'s Next Decade -- Herb Sutter

Registration is now open for CppCon 2024! The conference starts on September 15 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting some upcoming talks that you will be able to attend this year. Here’s another CppCon future talk we hope you will enjoy – and register today for CppCon 2024!

Peering Forward - C++'s Next Decade

Monday, September 16 08:45 - 10:30 MDT

by Herb Sutter

Summary of the talk:

This is an exciting year for ISO C++: In just the past few months, it has started to become clear that C++ is approaching three major positive turning points that are starting to materialize together in a blossoming of usability we haven’t seen since C++11.

First, compile-time reflection, including source generation, will dominate the next decade of C++ as arguably the most powerful feature that we’ve ever standardized, and (fingers crossed!) it’s on track for being included in C++26 in the coming months. I expect reflection’s impact on library building to be comparable to that of all the other library-building improvements combined that we’ve added since C++98.

  • Related: The CppCon 2024 Friday keynote will be all about reflection… more about that will be announced soon!

Second, memory safety is being taken seriously in WG21. After a decade or two of gradual smaller improvements, the committee is actively working toward taking the major step of enabling well-known proven-effective safety checks at compile time by default, without compromising performance.

  • Related: The CppCon 2024 Monday evening panel and Wednesday keynote will be all about safety… more about those will be announced soon!

Third, simplifying C++ is being taken seriously. I’m not the only person actively proposing simplifications to C++, and I expect the rate of simplification proposal papers to increase again in the coming year as the fruits of in-the-field experiments turn into evidence that the experimental improvements are working and are ready to be considered for ISO C++ itself to benefit all programmers.

Most of all, the above overlap and reinforce each other. For example, reflection will enable writing more new facilities as compile-time libraries instead of as language features that have to be baked into a compiler, which helps simplify future language evolution. Reflection will also enable compile-time libraries that let developers express their intent directly and leave it to the library code to accurately generate correct implementations, which helps reduce errors and makes our code both simpler and safer.

ISO C++ has long been solidly in the top 5 programming languages and is going strong. This talk presents reasons to expect that C++’s future is bright, and that perhaps its most important decade is just ahead.

 

Herb is an author, designer of several Standard C++ features, and chair of the ISO C++ committee and the Standard C++ Foundation. His current interest is simplifying C++.

PVS-Studio 7.32: enhanced analysis, new plugins and more

PVS-Studio 7.32 has been released. Discover enhanced C++ analysis optimization, new plugins and features, and a host of other updates.

PVS-Studio 7.32: enhanced analysis, new plugins and more

by Gleb Aslamov

From the article:

The PVS-Studio analyzer now supports integration with Bazel and Scons build systems for C++ projects. The PVS-Studio plugin is now available for Qt Creator 14.x. The plugin for Qt Creator 8.x is no longer supported. We aim to ensure backward compatibility between the latest plugin versions and all Qt Creator versions released in the past two years.

CppCon 2024 Perspectives on Contracts -- Lisa Lippincott

Registration is now open for CppCon 2024! The conference starts on September 15 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting some upcoming talks that you will be able to attend this year. Here’s another CppCon future talk we hope you will enjoy – and register today for CppCon 2024!

Perspectives on Contracts

Thursday, September 19 14:00 - 15:00 MDT

by Lisa Lippincott

Summary of the talk:

For many years, members of the C++ working group, WG21, have tried to add support for contract assertions — preconditions, postconditions, and assertion statements within a function body — to the C++ language. It’s been a long and difficult road, but we now have a proposal (P2900, “Contracts for C++”) to add significant contract support in, we hope, C++26, with more complete support to follow in C++29.

Looking back, the contracts feature has been unusually difficult and contentious. I contend that the difficulty is inherent in the feature: a contracts facility, by its nature,  cannot be understood from any single point of view.

In this lecture, I will explain why understanding contracts requires seeing the feature from a panoply of perspectives, and show how shifting between these perspectives allows one to make effective use of the facility.

This lecture is derived from presentations made to the Language Evolution subgroup of WG21 at the St. Louis meeting in June 2024.

 

Lisa Lippincott designed the software architectures of Tanium and BigFix, two systems for managing large fleets of computers. She's also a language nerd, and has contributed to arcane parts of the C++ standard. In her spare time, she studies mathematical logic, and wants to make computer-checked proofs of correctness a routine part of programming.