June 2023

CppCon 2022 Using Modern C++ to Eliminate Virtual Functions -- Jonathan Gopel

usingmoderncppto-gopel.pngRegistration is now open for CppCon 2023! The conference starts on October 1 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2023!

Using Modern C++ to Eliminate Virtual Functions

by Jonathan Gopel

Summary of the talk:

As of C++20, there are no cases in which statically linked programs require virtual functions. This talk will explore techniques for replacing runtime polymorphism with compile-time polymorphism such that virtual functions are never necessary. This talk will also address the higher-order concern of when it might make sense to avoid virtual functions or remove them from a codebase, as that decision ultimately is a design decision that only the author of the code can make. Attendees can expect to come away with a stronger understanding of the purposes of virtual functions and the mechanisms in modern C++ that can now be used to achieve those same purposes.

Why You Should Only Rarely Use std::move -- Andreas Fertig

andreas-fertig.pngstd::move can allow the efficient transfer of resources from object to to object. Andreas Fertig reminds us that using std::move inappropriately can make code less efficient.

Why You Should Only Rarely Use std::move

by Andreas Fertig

From the article:

The example in Listing 1 is the code I used to make my point: don’t use std::move on temporaries! Plus, in general, trust the compiler and only use std::move rarely. For this article, let’s focus on the example code.

class S {
public:
  S() { printf("default constructor\n"); }
  ~S() { printf("deconstructor\n"); }
  // Copy constructor ①
  S(const S&) { printf("copy constructor\n"); }
  // Move constructor ②
  S(S&&) { printf("move constructor\n"); }
};
void Use()
{
  S obj{
    S{} // Creating obj with a temporary of S ③
  };
}
Listing 1

Here we see a, well, perfectly movable class. I left the assignment operations out. They are not relevant. Aside from the constructor and destructor, we see in ① the copy constructor and in ② the move constructor. All special members print a message to identify them when they are called.

Further down in Use, we see ③, a temporary object of S used to initialize obj, also of type S. This is the typical situation where move semantics excels over a copy (assuming the class in question has movable members). The output I expect, and I wanted to show my participants, is:

Type Safe C++ enum Extensions -- Alf Steinbach

steinbach1.jpgIs it possible to extend a value type in C++? Alf Steinbach describes how to extend enum values.

Type Safe C++ enum Extensions

by Alf Steinbach

From the article:

Consider if an enum like the following,

  enum class Suit{
  spades, hearts, diamonds, clubs };

could be extended like

  enum class Suit_with_joker extends Suit {
    joker };

where

  • Suit_with_joker has all the enumerators of Suit plus the joker enumerator; and
  • enumerators introduced in Suit_with_joker get integer values following those of Suit; and
  • any Suit value is also a Suit_with_joker value.

This would be an example of what I’ll call a value type extension.

The apparently backwards is-a relationship in the last point, where any value of the original type is-a value of the derived type, is characteristic of value type extensions.

C++20 totally lacks support for value type extensions, of enum types or other types.

CppCon 2022 Finding Whether a Number is a Power of 2 -- Ankur Satle

Finding_Whether_a_Number_is_a_Power_of_2_Ankur_Satle.pngRegistration is now open for CppCon 2023! The conference starts on October 1 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2023!

Lightning Talk: Finding Whether a Number is a Power of 2

by Ankur Satle

Summary of the talk:

I asked this question on social media and got many diverse responses. I will present the various approaches and compare them. I will finish with the options C++20 provides with the bit header. https://en.cppreference.com/w/cpp/numeric/has_single_bit

CppCon 2022 Who is Looking for a C++ Job? -- Jens Weller

whoislookingforjob-weller.pngRegistration is now open for CppCon 2023! The conference starts on October 1 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2023!

Lightning Talk: Who is Looking for a C++ Job?

by Jens Weller

Summary of the talk:

A few insights on who has been applying for C++ jobs and visited the Meeting C++ online job fairs.

It’s Great That You Provide Operator Overloads, But It’s Also Nice to Have Names -- Raymond Chen

RaymondChenPic.pngOperator overloading. Looks great. Reduces verbosity. Until it doesn’t.

It’s Great That You Provide Operator Overloads, But It’s Also Nice to Have Names

by Raymond Chen

From the article:

Consider this overloaded function call operator:

struct StorageLoader
{
    template<DataType>
    DataType operator()(StorageOptions<DataType> const* options);
};

The idea is that you can use the function call operator on a Storage­Loader object to load data from storage, using a StorageOptions to describe how you want it to be loaded.

 

CppCon 2022 Template Meta-Programming in C++ -- Kris Jusiak

Template_Meta-Programming_in_Cpp_Kris_Jusiak.pngRegistration is now open for CppCon 2023! The conference starts on October 1 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2023!

Lightning Talk: MP: Template Meta-Programming in C++

by Kris Jusiak

Summary of the talk:

In this lightning talk, Kris attempts to teach template meta-programming in 5 minutes with the use of C++20 and ranges.
 

Object Lifetime -- Ilya Doroshenko

Today, we are going to talk about an object. Without further ado, let’s dive deeper!

Object Lifetime

by Ilya Doroshenko

From the article:

What is an object? According to the C++ standard, part 3.9.8 under the name of [basic.types]

 An object type is a (possibly cv-qualified) type that is not a function type, not a reference type, and not a void type.

Now is int i an object? Yes.

Is void* p an object? Still yes, because pointers are types themselves, they are not references.

As we said, references are not types, but what if we declare something like struct S{ int& ref;}; would that be an object type? 

 

CppCon 2022 Back to Basics: Templates in C++ -- Nicolai Josuttis

CppCon_2022_Back_to_Basics_Templates_Nicolai_Josuttis.pngRegistration is now open for CppCon 2023! The conference starts on October 1 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2023!

Back to Basics: Templates in C++

by Nicolai Josuttis

Summary of the talk:

Templates are among the most powerful features of C++, but they remain misunderstood and underutilized, even as the C++ language and development community have advanced. This talk shows when and how to use modern templates to build software that's cleaner, faster, more efficient, and easier to maintain. We will cover all basic aspects of templates you have to know when programming generic code in Modern C++

Dealing with Mutation: Guarded Suspension -- Rainer Grimm

dealingwithmutation.pngGuarded Suspension applies a unique strategy to deal with mutation. It signals when it is done with its modification.

Dealing with Mutation: Guarded Suspension

by Rainer Grimm

From the article:

The guarded suspension basic variant combines a lock and a precondition that must be satisfied. If the precondition is not fulfilled, that checking thread puts itself to sleep. The checking thread uses a lock to avoid a race condition that may result in a data race or a deadlock.

Various variants of the Guarded Suspension exist:

  • The waiting thread can passively be notified about the state change or actively ask for the state change. In short, I call this push versus pull principle.
  • The waiting can be done with or without a time boundary.
  • The notification can be sent to one or all waiting threads.

I present in this post only the rough idea. Let me start with the push principle.

You often synchronize threads with a condition variable or a future/promise pair. The condition variable or the promise sends the notification to the waiting thread. A promise has no notify_one or notify_all member function. Typically, a valueless set_value call is used to ...