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 through maps An excellent demonstration of structured bindings is an iteration through a map objec...| C++ Stories
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:| C++ Stories
A single-precision floating-point number is represented by 32 bits and hides various wonderful encoding techniques. However, some of those tricks might cause some imprecise calculations so it’s crucial to know how to work with those numbers. Let’s have a look at three common misconceptions. This is a guest post from Adam Sawicki| C++ Stories
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.| C++ Stories
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.| C++ Stories
Modern C++ continuously improves its range library to provide more expressive, flexible, and efficient ways to manipulate collections. Traditionally, achieving tasks like concatenation and flattening required manual loops, copying, or custom algorithms. With C++’s range adaptors, we now have an elegant and efficient way to process collections lazily without unnecessary allocations.| C++ Stories
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::default_accessor > class mdspan; And it has its own header .| C++ Stories
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 due to multiple memory allocations.| C++ Stories
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 manager object that stores various configs in a map and provides a method to retrieve them.| C++ Stories
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.| C++ Stories
C++23 Library Features and Reference Cards In this blog post, you’ll see all C++23 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 This article is “in progress”, most topics are completed, but a few sections are “to-do”.| C++ Stories
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! 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 description and an example if possible. All of the existing subscribers of my mailing list have already got the new document, so If you want to download it just subscribe here: Download a free ...| C++ Stories
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 library.| C++ Stories
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.| C++ Stories
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 improvements like constexpr support and enhanced error handling.| C++ Stories
In this article, you’ll learn why std::initializer_list has a bad reputation in C++. Is passing values using is 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.| C++ Stories
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.| C++ Stories
The evolution of the C++ language continues to bring powerful features that enhance code safety, readability, and maintainability. Among these improvements, we got changes and additions to enum class functionalities across C++17, C++20, and C++23. In this blog post, we’ll explore these advancements, focusing on initialization improvements in C++17, the introduction of the using enum keyword in C++20, and the std::to_underlying utility in C++23.| C++ Stories
Working with the filesystem can be a daunting task, but it doesn’t have to be. In this post, I’ll walk you through some of the most common filesystem operations using the powerful features introduced in C++17, as well as some new enhancements in C++20/23. Whether you’re creating directories, copying files, or managing permissions, these examples will help you understand and efficiently utilize the std::filesystem library.| C++ Stories
In this blog post, we’ll examine the potentially simple task of getting and displaying file time. Usually, we can depend on some library or OS-specific code to get that file property. Fortunately, since C++20, we have a reliable and simple technique from the std::filesystem and std::chrono components. Let’s jump in.| C++ Stories
Although C++ is an old programming language, its Standard Library misses a few basic things. Features that Java or .NET had for years were/are not available in STL. With C++17 there’s a nice improvement: for example, we now have the standard filesystem! Traversing a path, even recursively, is so simple now!| C++ Stories
In this blog post, we’ll show how to implement a custom pipe operator and apply it to a data processing example. Thanks to C++23 and std::expectedwe can write a rather efficient framework that easily handles unexpected outcomes. This is a collaborative guest post by prof. Bogusław Cyganek: Prof. Cyganek is a researcher and lecturer at the Department of Electronics, AGH University of Science and Technology in Cracow, Poland.| C++ Stories
std::expected from C++23 not only serves as an error-handling mechanism but also introduces functional programming paradigms into the language. In this blog post, we’ll have a look at functional/monadic extensions of std::expected, which allow us to chain operations elegantly, handling errors at the same time. The techniques are very similar to std::optional extensions - see How to Use Monadic Operations for `std::optional` in C++23 - C++ Stories.| C++ Stories
In the article about std::expected, I introduced the type and showed some basic examples, and in this text, you’ll learn how it is implemented. A simple idea with struct In short, std::expected should contain two data members: the actual expected value and the unexpected error object. So, in theory, we could use a simple structure:| C++ Stories
In this article, we’ll go through a new vocabulary type introduced in C++23. std::expected is a type specifically designed to return results from a function, along with the extra error information. Motivation Imagine you’re expecting a certain result from a function, but oops… things don’t always go as planned:| C++ Stories
Have you ever used the pimpl idiom in your code? No matter what’s your answer read on :) In this article I’d like to gather all the essential information regarding this dependency breaking technique. We’ll discuss the implementation (const issue, back pointer, fast impl), pros and cons, alternatives and also show examples where is it used.| C++ Stories
In this article, we’ll look at std::span which is more generic than string_view and can help work with arbitrary contiguous collections. A Motivating Example Here’s an example that illustrates the primary use case for std::span: In traditional C (or low-level C++), you’d pass an array to a function using a pointer and a size like this:| C++ Stories
In this post we’ll have a look at new operations added to std::optional in C++23. These operations, inspired by functional programming concepts, offer a more concise and expressive way to work with optional values, reducing boilerplate and improving code readability. Let’s meet and_then(), transform() and or_else(), new member functions. Traditional Approach with if/else and optional C++20 In C++20 when you work with std::optional you have to rely heavily on conditional checks to ensur...| C++ Stories
In this article, we’d shed some light on the implementation of ranges::reverse_view and std::views::reverse. We’ll compare them to understand the differences between views and their adaptor objects. Let’s jump in. ranges::reverse_view and std::views::reverse in Action Let’s look at an example to understand how these views work. Assume we have a range r of integers from 1 to 5.| C++ Stories
A few years ago (see here, I showed an interesting implementation for self-registering classes in factories. It works, but one step might be at the edge of Undefined behavior. Fortunately, with C++20, its new constinit keyword, we can update the code and ensure it’s super safe. Intro Let’s bring back the topic:| C++ Stories
The ISO Committee accepted and published the C++17 Standard in December 2017. In this mega-long article, I’ve built (with your help!) a list of all major features of the new standard. Please have a look and see what we get! Language Features New auto rules for direct-list-initialization static_assert with no message typename in a template template parameter Removing trigraphs Nested namespace definition Attributes for namespaces and enumerators u8 character literals Allow constant evaluatio...| C++ Stories
Conceptually a Range is a simple concept: it’s just a pair of two iterators - to the beginning and to the end of a sequence (or a sentinel in some cases). Yet, such an abstraction can radically change the way you write algorithms. In this blog post, I’ll show you a key change that you get with C++20 Ranges.| C++ Stories