This article explains the two semaphore types introduced in C++20: std::counting_semaphore and std::binary_semaphore . We’ll first use a counting semaphore to limit how many threads can operate at the same time. Then we’ll use a binary semaphore to send a signal between threads. We’ll also look at timed waiting, a small RAII helper, and a few more details. Note: The synchronization features…
Modern C++ continuously improves its range library to provide more expressive, flexible, and efficient ways to manipulate collections. Usually, when you wanted to concatenate or flatten ranges, you’d use raw loops or custom algorithms. With C++’s range adaptors, we now have an elegant and efficient way to process collections lazily without unnecessary allocations. In this post, we will…
Do you know how many ways we can implement a filter function in C++? While the problem is relatively easy to understand - take a container, copy elements that match a predicate, and return a new container - it’s good to exercise with the C++ Standard Library and check a few ideas. We can also apply some Modern C++ techniques, including C++23. Let’s start! The article was written in…
In this article, we’ll start with a basic example using std::mutex , look at its limitations, and then introduce std::shared_mutex , a reader-writer mutex added in C++17. Even in 2026, with many new concurrency features available, std::shared_mutex is still a valuable and practical tool. Let’s jump in. A Simple Thread-Safe Counter with std::mutex We’ll begin with a small example (a standard…
What do you do when the code for a variable initialization is complicated? Do you move it to another method or write inside the current scope? In this blog post, I’d like to present a trick that allows computing a value for a variable, even a const variable, with a compact notation. Updated in Jan 2026: improved code, added C++26 section, added [&] section, updated code samples and added links to…
This article collects small, self-contained, and practical examples for working with std::chrono calendar types. The previous blog post - see Exploring C++20 std::chrono - Calendar Types - C++ Stories - focused on the building blocks: calendar types, operators, and arithmetic rules. In this post, we’ll focus on practical examples like: What’s the last business day of the month? When is the…
Before C++20, <chrono> gave us a solid foundation for working with clocks, durations, and time points - but it didn’t really know anything about the civil calendar. If you needed to represent “March 15”, check whether a date is valid, compute “the third Monday of November”, or add a few months to a date, you had to rely on custom utilities, std::tm , or external libraries like Howard Hinnant’s…
In part 1 of this mini-series, we looked at the basics of iterating over a std::tuple using index_sequence and fold expressions. In part 2 , we simplified things with std::apply and even created helpers like for_each_tuple and transform_tuple . So far, we used C++ features up to C++20/23… but now, in C++26, we finally get language-level tools that make tuple iteration straightforward and…
In the previous article on the tuple iteration, we covered the basics. As a result, we implemented a function template that took a tuple and could nicely print it to the output. There was also a version with operator << . Today we can go further and see some other techniques. The first one is with std::apply from C++17, a helper function for tuples. Today’s article will also cover some…
Structured binding is a C++17 feature that allows you to bind multiple variables to the elements of a structured object, such as a tuple or struct. This can make your code more concise and easier to read, especially when working with complex data structures. On this blog, we already covered this functionality, but we’ll talk about some good C++26 additions and real code use cases. Iterating…
In this blog post, we’ll look at static variables defined in a function scope. We’ll see how they are implemented and how to use them. What’s more, we’ll discuss several cases where we can avoid extra thread-safety cost. Let’s start. Introduction As you may know, C++ offers a way to define static variables in a function/block scope: void foo () { static int counter =…
In this blog post, we’ll continue looking at ranges and this time explore ways to split them into sub-ranges. So we’ll take a look at views::split , views::chunk , and views::chunk_by . We’ll walk through two examples for each adaptor: one simple and one slightly more advanced, to highlight their practical uses. Let’s go. Splitting Ranges with views::split , C++20 If you want to…
In this blog post, we’ll write an iterator that works with a vector of vectors. We’ll explore a “manual” version as well as leverage C++20 ranges/views to do the hard work. The problem statement What’s the use case? See this popular interview question (found in DailyCodingProblem: https://www.dailycodingproblem.com/ ) Implement a 2D iterator class. It will be…
In this article, we’ll see details of std::mdspan, a new view type tailored to multidimensional data. We’ll go through type declaration, creation techniques, and options to customize the internal functionality. Type declaration The type is declared in the following way: template < class T , class Extents , class LayoutPolicy = std :: layout_right , class AccessorPolicy = std ::…
In graph theory, an adjacency matrix is a square matrix used to represent a finite (and usually dense) graph. The elements of the matrix indicate whether pairs of vertices are adjacent or not, and in weighted graphs, they store the edge weights. In many beginner-level tutorials, adjacency matrices are implemented using vector of vectors (nested dynamic arrays), but this approach has inefficiencies…
In this article, we’ll look at std::span , which has been available since C++20. This “view” type is more generic than string_view and can help work with arbitrary contiguous collections. Updated in Feb 2025: added section about returning spans and C++26 improvements ( .at() and creatoion from initializer list). A Motivating Example Here’s an example that illustrates the…
In this blog post, we’ll explore ways to improve the safety of a simple configuration manager. We’ll handle common pitfalls like dangling references and excessive stack usage. Additionally, we’ll see how C++26 helps enforce safer coding practices with stricter diagnostics and improved handling of large objects. Let’s go. Step 1: The Buggy Implementation Below is a simple example of a…
In this article, you’ll see eight larger examples that illustrate the changes in C++23. C++23 brings a ton of cool features, as you can see in my two previous articles ( here and here ). So far, we explored each new addition one by one, but I’d like to share more examples that combine multiple features. Here we go: 1. std::ranges::to<> The ranges::to<> feature allows you to convert…
In this blog post, you’ll see all C++23 library features! Each with a short description and additional code example. Prepare for a ride! For language features please see the previous article: C++23 Language Features and Reference Cards - C++ Stories Want your own copy to print? If you like, I prepared PDF I packed both language and the Standard Library features. Each one has a short…
C++23 Language Features In this blog post, you’ll see all C++23 language features! Each with short description and additional code example. Prepare for a ride! For library features please see the next article: C++23 Library Features and Reference Cards - C++ Stories Want your own copy to print? If you like, I prepared PDF I packed both language and the Standard Library features. Each one has…
While most time zones use simple hour offsets from UTC, some regions have chosen unusual time differences. In this blog post, we’ll explore how we can discover such zones using C++20’s chrono library. We’ll use GCC 14.2 as it fully supports C++20 chrono and also std::print from C++23. First Attempt: Basic Zone Iteration C++20 introduced comprehensive time zone support through the…
In this blog post, we will explore handling dates using std::chrono , including time zones. We’ll utilize the latest features of the library to retrieve the current time across various time zones, taking into account daylight saving time changes as well. Additionally, we will incorporate new capabilities introduced in C++23, such as enhanced printing functions and more. Let’s start…
With the introduction of C++17, the C++ Standard Library expanded its capabilities for converting text to numbers with the addition of std::from_chars . This low-level, high-performance API offers significant advantages over previous methods, such as atoi and stringstream . In this article, we will explore the evolution of string conversion routines from C++17 through C++26, highlighting key…
In this article, you’ll learn why std::initializer_list has a bad reputation in C++. Is passing values using it as efficient as “emplace”? How can we use non-copyable types? You’ll also see how to fix some of the problems. Let’s start with the issues first: Updated in Sept 2024: Added note about stack overflow and C++26 fixes. This is the second part of…
Imagine you’re developing a tool that needs to scan for file changes across thousands of project files. Retrieving file attributes efficiently becomes critical for such scenarios. In this article, I’ll demonstrate a technique to get file attributes that can achieve a surprising speedup of over 50+ times compared to standard Windows methods. Let’s dive in and explore how we can…