This bite-sized article explores the latest Standard’s support for Python-style f-strings (having a literal f prefix) in C++. This may seem like a strange feature to add, but if you use Python, chances are you already find them invaluable when mixing (or interpolating) values, such as variables and expressions, with static output, such as descriptions. So without further ado, let’s jump straight into the syntax with some simple applications.
Tag: Modern C++
Language-level assertions in C++26
Assertions (run-time checks of a Boolean condition) have been around since the beginnings of the C language, and are of course available to C++ from the <cassert> header. Starting with the latest Standard, C++ supports a more flexible and featured approach to assertions, with the new language keywords pre, post and contract_assert. This article is intended to provide a walkthrough and quick-start to using these keywords in Modern C++ code, and hopefully explains some of the aims and rationale behind their names and functionality.
Reflection in C++26 (P2996)
This article is intended to summarize the state of play in the upcoming C++26 Standard as regards reflection, which is where information (metadata) about the source code is made available for use at run-time, as opposed to only being available at compile-time. Proposal P2996 is the main (initial) paper for the implementation of reflection in C++, and has been voted into C++26 as of June 2025. An “experimental” branch of Clang (available on Compiler Explorer) currently implements a number of features from this paper (note that not all of the code in this article compiles at present, as syntax is in a state of flux).
Continue reading “Reflection in C++26 (P2996)”Move Semantics in Modern C++ (2)
Now that we’ve looked at how std::move() can be applied to Standard Library types, such as std::string, in this article we’re going to look at creating our own “move-aware” type. We want it to have at least one member for which moving is potentially cheaper than copying, and for this we’ll use a raw char* pointer. We also want to log all uses of the five special member functions (constructor, copy-constructor, move-constructor, copy-assignment, and move-assignment) in order to understand exactly what is happening.
Move Semantics in Modern C++ (1)
The title for this mini-series may seem to be ambitious for two reasons: move semantics have been available for a long time (since C++11 in fact, so not very “Modern”), and it is a large subject (there is a book by a well-regarded author devoted entirely to the topic). We’ll be introducing the subject slowly, starting with the core concepts and notation, before moving onto the interesting corner-cases. In this article we’ll begin by discussing the idea of a “movable” type and the implications for performance gains.
Continue reading “Move Semantics in Modern C++ (1)”Operator Overloading in Modern C++ (2)
Having looked at how to create a class representing a three-dimensional vector in the previous article, we’re going to look at how to add more operator-related functionality to it. We’ll start off with multiplying and dividing by a scalar, and move on to providing non-member operator overloads.
Continue reading “Operator Overloading in Modern C++ (2)”Operator Overloading in Modern C++ (1)
OO (or OOP) has been used to mean “Object-Oriented (Programming)” for several decades, but there is another use of the acronym OO which is: “Operator Overloading”. Simply put this involves creating (concrete) classes (or types) for which some (or rarely, most, or even all) C++ operators are redefined in terms of functionality related to the class. Since its appearance in the early 1990s, the operator keyword in C++ has been used in numerous applications to allow built-in operators such as +, -= and even -> to be overloaded on a per-class basis. This mini-series intends to demonstrate syntax, techniques, and modern best practices for OO in C++; in this article we’ll start to create a class representing a three-dimensional vector and give it state and functionality.