2024

What's So Hard About Cass Types as Non-type Template Parameters? -- Barry Revzin

revzin200930.pngPreviously, I tried to answer the question: what’s so hard about constexpr allocation?. Today, we continue what will become a series of posts about attempting to explain the issues behind a bunch of hard problems we’re trying to solve. The next problem: class types as non-type template parameters.

What's So Hard About Cass Types as Non-type Template Parameters?

by Barry Revzin

From the article:

Before C++20, the only types you could use as non-type template parameters were scalar types (like int and enums, but not floating point), pointers (including pointers-to-members), and references. Notably, not class types.

Floating point types were also added in C++20.

The hard question about handling class types is: how do you determine template argument equivalence? Class types can define their own equality. But you don’t really want to rely on that, since now matching specializations becomes a quadratic problem — you can’t really do much when determining the instantiation for some template f<x> other than looping through every other value vi to see if x == vi.

The other problem is we need == to really be strong enough. One very easy problem to run into in this space is violating the One Definition Rule (ODR). One essential, core requirement for templates is that instantiating the same template with the same arguments has to yield the same code.

 

Highlighting the student and support tickets for Meeting C++ 2024

Meeting C++ offers free online and onsite tickets through their student and support programs. This is supported through the ticket sales for Meeting C++ 2024.

Highlighting the student and support tickets for Meeting C++ 2024

by Jens Weller

From the article:

Like every year, I'd like to point towards the programs for those that can't afford to pay for a ticket for Meeting C++ 2024.

And let me start with thanking those that enable these programs through their ticket buying: the attendees and sponsors of Meeting C++ 2024! With the schedule published, I'd like to highlight the student and support tickets for Meeting C++ 2024. For a few years now Meeting C++ has hosted programs to give students, underrepresented folks and those who can't afford a ticket access to the conference.

This year for the first time you can choose to register either for onsite or online for these programs. A limited amount of tickets for Berlin will be available, depending on the ticket sale in October. So incase you registered for onsite but are not chosen for a ticket, then you'll have a chance to an online ticket...

Trip report: C++ On Sea 2024 -- Sandor Dargo

logo.pngC++ On Sea took place in Folkestone again in February this year. Sandor Dargo shares an overview of his favourite talks and some emergent ideas.

Trip report: C++ On Sea 2024

by Sandor Dargo

From the article:

Last week, between the 3rd and 5th of July, I had the privilege of attending and presenting at C++ on Sea 2024 [CPPoS-1] for the 5th time in a row! I’m grateful that the organizers accepted me not simply as a speaker, but that they granted me a double slot to deliver a half-day workshop about how to reduce the size of a binary. I’m also thankful for my management that they gave me the time to go to Folkestone and share our knowledge on binary size. Last but not least, great thanks goes to my wife, who took care of the kids alone that week.

Let me share with you a few thoughts about the conference.

First, I’m going to write about the 3 talks that I liked the most during the 3 days, then I’m going to share 3 interesting ideas I heard about and then I’m going to share some personal impressions about the conference.

IoT Development with POCO C++ libraries and macchinaio

Recently Günter Obiltschnig from the POCO Project gave a talk about IoT Development with POCO C++ libraries and macchinaio at a by macchina.io sponsored Meetup of Meeting C++ online.

IoT Development with POCO C++ libraries and macchinaio

by Günter Obiltschnig

Chapter Videos:

POCO C++ Libraries overview

Macchina.io overview

Projects using POCO C++ Libaries and macchina.io in the real world

Q&A with Günter Obiltschnig after the talk

std::array in C++ isn't slower than array in C

In my previous article on arrays, some readers expressed concern that std::array might be slower than the built-in C array. Several sources of truth exist on this matter, and today we'll go through each of them. Let's first find out what the standard states about it, then look at the std::array implementations in libc++ and libstdc++, and finally look at the assembler of some operations on these objects. Oh, and we'll top it off with benchmarking, of course.

std::array in C++ isn't slower than array in C

by Anton Tretyakov

From the article:

Let's get to the bottom of this. LLVM has a hardening mechanism called _LIBCPP_HARDENING_MODE. We can use it to enable additional checks depending on the mechanism level, which has a total of four levels. Enabling the weakest one removes the checks from the code. In other cases, there may or may not be a check, depending on the check and the level of the mode. We'll prove it. To understand what expands to what, we need to look at the source code. There, we see that depending on the given value of _LIBCPPP_HARDENING_MODE, _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS may expand to _LIBCPPP_ASSERT.

In an Atomic World -- Lucian Radu Teodorescu

logo.pngAtomics form a relatively low level, but fundamental part of sharing data across threads. Lucian Radu Teodorescu reminds us what atomics are and how and when to use them.

In an Atomic World

by Lucian Radu Teodorescu

From the article:

We often discuss mutexes as the basic building blocks of concurrency. However, there are more fundamental concepts upon which concurrent programs and synchronization primitives are constructed. The C++ language defines a memory model, which describes how programs behave when multiple threads are involved. Additionally, C++ introduces atomic operations that serve as foundation for working with data across threads, ensuring both safety and performance. The goal of C++ atomics is to closely align with the hardware and eliminate the need for lower-level operations that must work across threads.

The topic of atomics is often overlooked, and the prevailing advice is to avoid them. While this advice is generally sound, there are occasions when we need to use atomics to fully leverage the language’s capabilities. This article aims to give atomics the attention they deserve, as they have yet to be featured in an Overload article.

The subject of atomics is extensive. For a comprehensive exploration, readers are encouraged to consult books by Anthony Williams [Williams19] and Mara Bos [Bos23]. While the Bos book primarily focuses on Rust, there is still much to be learned about atomics for C++ programmers. The reader can also consider cppreference.com for a quick reference to the atomics library [cppreference-1] In this article, we will examine various memory ordering models and illustrate their usage through simplified practical examples.

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

Your attention is invited to the sixth 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 6 of 11

by Dmitry Sviridkin

From the article:

I/O streams have other flags that represent the state of the stream: whether there were errors, whether we reached the end. Many people know that you can check whether an operation was successful by putting a stream object into a conditional statement (or any context where it is converted to bool). Those unfamiliar with it might use the while (!iss.eof()) check that will one day lead to the infinite loop issue. This happens when the file isn't finished, but can no longer be read—say, if the file is on a network drive, and the network has gone down. Well, that's a story for another time. Let's focus on the correct way to check readability.

Meeting C++ 2024: the online track is complete

The last part of the program for Meeting C++ 2024 is now ready: the online track.

The online track for Meeting C++ 2024 is complete!

by Jens Weller

From the article:

With this the program for Meeting C++ 2024 is now complete! The online track features 11 talks and will be prerecorded publically in October.

The talks of the online track are:

Temporarily Dropping a Lock: The Anti-lock Pattern -- Raymond Chen

RaymondChen_5in-150x150.jpgIn C++, it's common to use RAII types like std::lock_guard to manage synchronization primitives, ensuring a lock is acquired at object creation and released at destruction. However, a less common but useful pattern is the "anti-lock," which temporarily releases a lock and reacquires it later, useful in scenarios where you need to drop a lock while performing certain operations, like calling out to other components to avoid deadlocks.

Temporarily Dropping a Lock: The Anti-lock Pattern

by Raymond Chen

From the article:

There is a common pattern in C++ of using an RAII type to manage a synchronization primitive. There are different versions of this, but they all have the same basic pattern:

  • Creating the object from a synchronization object: Locks the synchronization object.
  • Destructing the object: Unlocks the synchronization object.

These types go by various names, like std::lock_guardstd::unique_lock, or std::coped_lock, and specific libraries may have versions for their own types, such as C++/WinRT’s winrt::slim_lock_guard and WIL’s wil::rwlock_release_exclusive_scope_exit (which you thankfully never actually write out; just use auto).

One thing that is missing from most standard libraries, however, is the anti-lock.

The idea of the anti-lock is that it counteracts an active lock.

CopperSpice: Template Design With Policy Classes

New video on the CopperSpice YouTube Channel:

Template Design With Policy Classes

by Barbara Geller and Ansel Sermersheim

About the video:

We have a new C++ video which discusses Policy Based Design and compares it to other styles of programming. Do you know which design pattern policy based programming solves? Have you considered the benefits of a design which provides a solution at compile time versus run time? Are you using policies and maybe you had no idea they had a name?

Please take a look and remember to subscribe.