May 2023

Why is std::hardware_destructive_interference_size a Compile-time Constant ... -- Raymond Chen

C++17 added a new compile time constant std::hardware_destructive_interference_size which tells you (basically) the size of a cache line. The purpose of this is to allow you to lay out your structures in a way that avoids false sharing.

Why is std::hardware_destructive_interference_size a Compile-time Constant Instead of a Run-time Value?

by Raymond Chen

From the article:

C++17 added a new compile time constant std::hardware_destructive_interference_size which tells you (basically) the size of a cache line. The purpose of this is to allow you to lay out your structures in a way that avoids false sharing.¹

But how does the compiler know what the cache line size will be of the CPU the program will eventually be run on? Shouldn’t this be a run-time value instead of a compile-time value?

Well yes, the actual size of the cache line isn’t know until run-time, because it is only then that the program meets a CPU. But really, you want this to be a ...

CppCon 2022 Can C++ be 10x Simpler > Safer? -- Herb Sutter

Cpp22-Sutter.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!

Can C++ be 10x Simpler & Safer? (Simplifying C++ #9 of N)

by Herb Sutter

Summary of the talk:

Since CppCon 2015, all of Herb’s talks have been about ways to evolve C++ to make it simpler, safer, and more toolable. Every release of ISO C++ has already been making regular incremental “10%” improvements in these areas. But what are the fundamental factors that limit our rate of improvement, and what would it take to make greater progress? Like every year, Herb’s talk will explore selected current pain points and describe experimental ideas to address them that might someday contribute toward C++’s long-term evolution.

Reactor -- Rainer Grimm

Event-driven applications, such as GUIs or servers, often apply the architecture pattern Reactor. A Reactor can accept multiple requests simultaneously and distribute them to different handlers.

Reactor

by Rainer Grimm

From the article:

The Reactor Pattern is an event-driven framework to concurrently demultiplex and dispatch service requests to various service providers. The requests are processed synchronously.

Problem

A server should

  • be extendable to support new or improved services
  • be performant, stable, and scalable
  • answer several client requests simultaneously

The application should be hidden from multi-threading and synchronization challenges

Solution

  • Each supported service is encapsulated in a handler
  • The handlers are registered within the Reactor
  • The Reactor uses an event demultiplexer to wait synchronously on all incoming events
  • When the Reactor is notified, it dispatches the service request to the specific handler

CppCon 2022 Best Practices Every C++ Programmer Needs to Follow -- Oz Syed

Cpp22-Syed.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: Best Practices Every C++ Programmer Needs to Follow

by Oz Syed

Summary of the talk:

In this session, learn some of the best practices that every C++ programmer needs to ensure successful completion of a project. Learn tips and tricks to speed up your workflow, prevent errors and improve performance.

Q&A - C++ Initialization -- Bartlomiej Filipek

Last time I showed a couple of questions about initialization. Try them here if you haven’t already. In this article, I’ll show you the answers and add more notes about initialization in C++.

Q&A - C++ Initialization

by Bartlomiej Filipek

From the article:

Will this code work in C++11?
struct User { std::string name = "unknown"; unsigned age { 0 }; };
User u { "John", 101 };
  1. Yes, the code compiles in C++11 mode.
  2. The code compiles starting with C++14 mode.
  3. The code doesn’t compile even in C++20.

CppCon 2022 C++ in Constrained Environments -- Bjarne Stroustrup

Cpp22-Stroustrup.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!

C++ in Constrained Environments

by Bjarne Stroustrup

Summary of the talk:

C++ is widely used in constrained and/or critical applications and infrastructure components. How do we manage to use a large multi-purpose language in such environments? How can we better use facilities and techniques from modern C++ (C++11, C++14, C++17, C++20, and beyond)? The best answer is not “use only facilities available in C and C++ in 1985.”

This talk focuses on a top-down approach to achieve simplicity, maintainability, performance, and various forms of safety. It touches upon the C++ Core Guidelines, compile-time computation, type-and-resource safety, type deduction, the span and chrono standard libraries, and error handling.

The Obvious Final Step -- Andrzej KrzemieĊ„ski

During the construction of an XML file when you write an element, it is obvious that the last thing that you do is to write the closing tag. By obvious we mean: writing it down adds no new information (there is no other possible final instruction for this task), and it would be a bug if this instruction wasn’t there.

The Obvious Final Step

by Andrzej Krzemieński

From the article:

The title may be misleading, as I had to invent a new short term for the pattern that occurs in the code once in a while. Example first:

// write an element of an XML file

xml.begin_element("port");
xml.attribute("name", name);
xml.attribute("location", loc);
xml.end_element("port");  // <-- the obvious final step

Defining Interfaces in C++: Concepts Versus Inheritance -- Daniel Lemire

DanielLemire.pngIn a previous blog post, Daniel Lemire showed how you could define ‘an interface’ in C++ using concepts and stated that he did not take into account inheritance as a strategy. He does so here.

Defining Interfaces in C++: Concepts Versus Inheritance

by Daniel Lemire

From the article:

In a previous blog post, I showed how you could define ‘an interface’ in C++ using concepts. For example, I can specify that a type should have the methods has_next, next and reset:

template <typename T>
concept is_iterable = requires(T v) {
                        { v.has_next() } -> std::convertible_to<bool>;
                        { v.next() } -> std::same_as<uint32_t>;
                        { v.reset() };
                      };

CppCon 2022 Dependency Injection for Modern C++ -- Tyler Weaver

Cpp22-Weaver.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: Dependency Injection for Modern C++

by Tyler Weaver

Summary of the talk:

DI is a fancy OO term with an even more complex set of tooling to solve a problem that higher order functions solve in a nicer way. In this talk I'll demonstrate using std::function for dependency injection and talk about how taking functions as a parameter is nicer and more first class in C++ than inheritance or complex mocking libraries.

Basic HTTP and WebSocket Programming with Boost.Beast -- Richard Thomson

Utah C++ Programmers has released a new video:

Basic HTTP and WebSocket Programming with Boost.Beast

by Richard Thomson

From the video description:

Boost.Beast is a C++ header-only library serving as a foundation for writing interoperable networking libraries by providing low-level HTTP/1, WebSocket, and networking protocol vocabulary types and algorithms using the consistent asynchronous model of Boost.Asio.

This month, Richard Thomson will give us an introduction to Beast and the facilities it provides for HTTP and WebSocket applications. We will look at an example of implementing a REST API for a simple CRUD (Create, Read, Update, Delete) style database. We will revisit the comic book database example we have used in previous REST API presentations.

https://www.youtube.com/watch?v=gVmwrnhkybk