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)”Author: cpptutor
New floating-point types in C++23
In common with the C language we have been used to having the float
, double
and long double
types available since the first versions of C++. (Did you know that long double
often uses 80 bits of precision, padded to 96 bits, on 32-bit platforms, but a full 128 bits on 64-bit platforms?) In this article we’re going to discuss the <stdfloat>
header new in C++23, which provides types with explicit precisions between 16 and 128 bits regardless of hardware platform, within the std::
namespace.
Move Semantics in Modern C++ (4)
In previous articles we’ve discussed how to make objects “move-aware” so that moves are more efficient than copies, and also how to forward objects efficiently between different scopes. In this article we’ll be looking at how different member function overloads can be called based on the (reference and const-ness) properties of the object itself.
Even novice C++ programmers may be familiar with the term “const-correctness” by its application to writing member functions with or without a const
qualifier (in other words, pairs of member functions: which one being called depending on the const-ness of the object). The amount of code duplication can be minimized by using the pattern popularized by Scott Meyers, where the non-const version calls the const version with a static_cast
of *this
(to const T&
), and further uses a const_cast
on any return value.
Move Semantics in Modern C++ (3)
In the previous two articles we looked at how to transfer “move-aware” objects, such as std::string
, between variables, and also how to create a Movable
class which logs all copy and move operations applied to it in order to gain a clearer picture of what is actually taking place. In this article we’re going to look at how to move objects into different scopes, these being caller and callee (function) scopes.
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++ (3)
With experience of how to overload common math operators gained from the previous articles, in this article we’re going to look at how to overload some “special” operators (unary *
and ->
) as well as increment and decrement (++
and --
). Since none of these could be usefully applied to our Vec3d
class, we’ll be creating a new class from scratch. We’ll also discuss three operators which should not normally be overloaded, even though the language permits it.
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.
Additional Unicode support in C++
Unicode is so prevalent these days that it is difficult to imagine any modern programming language not supporting it. C++ has made a number of attempts to provide language and library support for Unicode encodings (UTF-8/16/32), including conversions, display and manipulation, some of which are deprecated or removed. This article attempts to summarize the current state of Unicode support in C++23, and best practices to use when working with Unicode strings.
Continue reading “Additional Unicode support in C++”C++ Concurrency 101
Utilizing the potential of multi-core CPUs as found in virtually all modern hardware can only be achieved by writing multi-threaded programs. Code which is multi-threaded allows multiple threads to run through the same piece of code simultaneously, using different logical processors. The way this can be achieved is by allowing each thread to use a different stack frame and for the code segment being executed to be read-only; sharing read-only memory is safe. In this article we’ll cover the basics of concurrency as found in Modern C++, which by necessity can’t be the full story as entire books have been written on the subject. Only the high-level concepts and standard library building-blocks will be covered but hopefully this will provide a starting-point for readers new to either Modern C++ or the topic of concurrency.
Continue reading “C++ Concurrency 101”When to use std::string_view
C++17 brought us the class std::string_view
in the standard library, which was touted as a lightweight, non-owning string-like entity which could be passed cheaply as a function parameter. In this article we’re going to examine its best-practice use cases as well as where it’s neither necessary nor beneficial to use it.
Making algorithms faster
It is possible to write poor-quality code in any programming language, and this often stems from sub-optimal algorithms being used. Most of the execution time in modern software is spent in loops, so making these as efficient as possible is effort well spent. In addition, we want to utilize all of the CPUs available in a modern machine to maximize throughput. In this article we’ll perform a case-study of a simple algorithm being improved and parallelized, together with benchmarking to demonstrate the real-world gains.
Continue reading “Making algorithms faster”How could Reflection be applied to Modern C++?
Reflection is commonplace in other contemporary programming languages, but support for it in C++ is virtually non-existent. In this article we’re going to summarize what reflection is and how existing C++ techniques to implement it could potentially be improved in the upcoming C++26 Standard.
Reflection is the ability of a running program to inspect, analyze and modify its own structure at runtime. In C++ we would prefer to focus on compile-time reflection, which would not be expected to incur runtime costs. Existing features which do incur runtime costs already in C++ include RTTI (Run-Time Type Information) and virtual function dispatch, while those which do not include TMP (Template Metaprogramming) and compile-time function invocation.
Continue reading “How could Reflection be applied to Modern C++?”Pattern Matching is coming to Modern C++
Pattern matching is a commonly used idiom in other contemporary programming languages, such as Rust or Haskell, and is a way to inspect and deconstruct value(s) based on their type(s). Unlike destructuring in C++, which takes a single compound type and extracts the fields as separate variables, pattern matching is a way to deal with conditional logic based upon the type(s) involved. In this article we’ll be taking a look into the future (none of the code featured in this article compiles with trunk Clang or GCC at the time of writing) and consider what implications an adoption of this feature into the Standard might have for Modern C++.
Continue reading “Pattern Matching is coming to Modern C++”