Articles & Books

Mutating Through a Filter -- Barry Revzin

MutatingThroughaFilter.pngNico Josuttis gave a talk recently that included an example and I wanted to explain what’s going on in this example, what the issue is, and what (if anything) is broken.

Mutating Through a Filter

by Barry Revzin

From the article:

As with a lot of my explanations, we have to start from the beginning.

The C++ iterator model has a number of iterator categories: input, forward, bidirectional, random access, and (since C++20) contiguous. This post only needs to consider the first two.

An input range (a range whose iterator is an input iterator) is a single-pass range. You can only ever call begin() one time on it. You can’t have multiple different input iterators into the same range - incrementing one immediately invalidates any existing copies.

How To Check If A Pointer Is In A Range Of Memory -- Raymond Chen

RaymondChenPic.pngC language was defined to cover a large range of computer architectures, including many which would be considered museum relics today. It therefore takes a very conservative view of what is permitted, so that it remains possible to write C programs for those ancient systems. (Which weren’t quite so ancient at the time.)

How To Check If A Pointer Is In A Range Of Memory

by Raymond Chen

From the article:

Suppose you have a range of memory described by two variables, say,

byte* regionStart;
size_t regionSize;

And suppose you want to check whether a pointers lies within that region. You might be tempted to write

if (p >= regionStart && p < regionStart + regionSize)

but is this actually guaranteed according to the standard?

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 ...

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

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.

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() };
                      };

The Case of string_view and the Magic String -- Giovanni Dicanio

Someone learned about std::string_view, and started replacing instances of std::string const& with string_views in their C++ code base. As a result of that, a subtle bug shows up!

The Case of string_view and the Magic String

by Giovanni Dicanio

From the article:

(...) The code is recompiled and executed. But, unfortunately, now the output has changed! What’s going on here? Where does that “magic string” come from?

Sign Up for the free Pure Virtual C++ 2023 Conference -- Sy Brand

PureVirtualC++.pngEvery year we run Pure Virtual C++: a free one-day virtual conference for the whole C++ community. Next month we’re doing it again! Sign-up for free to get access to our five live sessions and a host of pre-conference content.

Sign Up for the free Pure Virtual C++ 2023 Conference

by Sy Brand

From the article:

The live event will run June 6th 13:00-16:00 UTC. Videos will be available to stream for free on YouTube after the conference.

The live sessions will be:

  • C++ Compiler Errors for Humans with Sy Brand
  • Address Sanitizer continue_on_error with Jim Radigan
  • Value-Oriented Programming with Tony Van Eerd
  • Productive Cross-Platform and Game Development in Visual Studio with Sinem Akinci and David Li
  • Build Time Reflection with C++ in Year 2023 with Gabriel Dos Reis