The above field is supplemented with consent to receive a newsletter containing information and marketing content about the cppstories.com portal from Bartłomiej Filipek codebf based in Krakow. The consent may be withdrawn at any time. See the full Privacy Policy.
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!
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.
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.
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:
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:
Thanks to the powerful constexpr keyword and many enhancements in recent C++ standards, we can now perform a lot of computations at compile time. In this text, we’ll explore several techniques for parsing integers, including the “naive” approach, C++23, from_chars, std::optional, std::expected, and even some upcoming features in C++26.
But first… why would this even be needed?
In this article, we’ll explore six practical string processing operations introduced in C++20 and C++23. These features represent an evolution in this crucial area, covering a spectrum of operations from searching and appending to creation and stream handling.
Let’s start with a simple yet long-awaited feature…
1. contains(), C++23 Finally, after decades of standardization, we have a super easy way to check if there’s one string inside the other.
std::format is a large and powerful addition in C++20 that allows us to format text into strings efficiently. It adds Python-style formatting with safety and ease of use.
This article will show you how to implement custom formatters that fit into this new std::format architecture.
Updated in Nov 2023: reflect the constness of the format() function, clarified in LWG issue 3636.
In this post, we’ll have fun using C++20’s spans to process data on multiple threads. What’s more, we’ll be equipped with the latest concurrency features from C++20.
This text was motivated by the following comment under my recent article on std::span:
But why does this article… not show the major use case?
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:
In this blog post, we’ll look at several different view/reference types introduced in Modern C++. The first one is string_view added in C++17. C++20 brought std::span and ranges views. The last addition is std::mdspan from C++23.
Let’s start.
String View (C++17) The std::string_view type is a non-owning reference to a string.
Learn how the overload pattern works for std::variant visitation and how it changed with C++20 and C++23.
While I was doing research for my book and blog posts about C++17 several times, I stumbled upon this pattern for visitation of std::variant:
template<class... Ts> struct overload : Ts... { using Ts::operator()...; }; template<class.