RSS Amplifier

Blog

C++ Stories

Recent content on C++ Stories

cppstories.comRSS feed ↗25 posts

Latest posts

Understanding std::counting_semaphore and std::binary_semaphore from C++20

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…

How to join or concat ranges, C++26

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…

15 Different Ways to Filter Containers in Modern C++

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…

Understanding std::shared_mutex from C++17

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…

IIFE for Complex Initialization

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…

7 Practical std::chrono Calendar Examples (C++20)

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…

Exploring C++20 std::chrono - Calendar Types

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…

C&#43;&#43; Templates: How to Iterate through std::tuple: C&#43;&#43;26 Packs and Expansion Statements

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&hellip; but now, in C++26, we finally get language-level tools that make tuple iteration straightforward and…

C&#43;&#43; Templates: How to Iterate through std::tuple: std::apply and More

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&rsquo;s article will also cover some…

Structured bindings in C&#43;&#43;17, 8 years later

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&rsquo;ll talk about some good C++26 additions and real code use cases. Iterating…

How to Avoid Thread-Safety Cost for Functions&#39; static Variables

In this blog post, we&rsquo;ll look at static variables defined in a function scope. We&rsquo;ll see how they are implemented and how to use them. What&rsquo;s more, we&rsquo;ll discuss several cases where we can avoid extra thread-safety cost. Let&rsquo;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 =…

How to Split Ranges in C&#43;&#43;23

In this blog post, we&rsquo;ll continue looking at ranges and this time explore ways to split them into sub-ranges. So we&rsquo;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&rsquo;s go. Splitting Ranges with views::split , C++20 If you want to…

Views as Data Members for Custom Iterators

In this blog post, we&rsquo;ll write an iterator that works with a vector of vectors. We&rsquo;ll explore a &ldquo;manual&rdquo; version as well as leverage C++20 ranges/views to do the hard work. The problem statement What&rsquo;s the use case? See this popular interview question (found in DailyCodingProblem: https://www.dailycodingproblem.com/ ) Implement a 2D iterator class. It will be…

Details of std::mdspan from C&#43;&#43;23

In this article, we&rsquo;ll see details of std::mdspan, a new view type tailored to multidimensional data. We&rsquo;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 ::…

Adjacency Matrix and std::mdspan, C&#43;&#43;23

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…

How to use std::span from C&#43;&#43;20

In this article, we&rsquo;ll look at std::span , which has been available since C++20. This &ldquo;view&rdquo; 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&rsquo;s an example that illustrates the…

Improving Code Safety in C&#43;&#43;26: Managers and Dangling References

In this blog post, we’ll explore ways to improve the safety of a simple configuration manager. We&rsquo;ll handle common pitfalls like dangling references and excessive stack usage. Additionally, we&rsquo;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…

8 More C&#43;&#43;23 Examples

In this article, you&rsquo;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&rsquo;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…

C&#43;&#43;23 Library Features and Reference Cards

In this blog post, you&rsquo;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&#43;&#43;23 Language Features and Reference Cards

C++23 Language Features In this blog post, you&rsquo;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…

Around the World in C&#43;&#43;: Exploring Time Zones with std::chrono

While most time zones use simple hour offsets from UTC, some regions have chosen unusual time differences. In this blog post, we&rsquo;ll explore how we can discover such zones using C++20&rsquo;s chrono library. We&rsquo;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…

What is the current time around the world? Utilizing std::chrono with time zones in C&#43;&#43;23

In this blog post, we will explore handling dates using std::chrono , including time zones. We&rsquo;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&rsquo;s start…

C&#43;&#43; String Conversion: Exploring std::from_chars in C&#43;&#43;17 to C&#43;&#43;26

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…

std::initializer_list in C&#43;&#43; 2/2 - Caveats and Improvements

In this article, you&rsquo;ll learn why std::initializer_list has a bad reputation in C++. Is passing values using it as efficient as &ldquo;emplace&rdquo;? How can we use non-copyable types? You&rsquo;ll also see how to fix some of the problems. Let&rsquo;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…

Boost File Scanning Speed: Query File Attributes on Windows 50x Faster

Imagine you&rsquo;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&rsquo;ll demonstrate a technique to get file attributes that can achieve a surprising speedup of over 50+ times compared to standard Windows methods. Let&rsquo;s dive in and explore how we can…