C++26 Contracts: What Do They Add Beyond Manual Checks and Assertions?
Explore C++26 Contracts through a practical ring-buffer example, including preconditions, postconditions, manual C++23 checks and GCC evaluation semantics.
Articles, examples and tutorials covering new features introduced in C++26.
Explore C++26 Contracts through a practical ring-buffer example, including preconditions, postconditions, manual C++23 checks and GCC evaluation semantics.
The post discusses exploring C++26, detailing setup on Ubuntu and features like template for, reflection, and JSON serialization. It introduces annotations as a powerful feature that simplifies validation in class constructors, ensuring values remain within specified ranges. This approach avoids error-prone manual validation when new class members are added.
The post discusses using the experimental C++26 reflection feature for JSON serialization of user-defined classes, specifically focusing on the CanDataSource class. It contrasts manual serialization approaches with the flexibility offered by reflection, allowing automatic updates in JSON output when class definitions change. Full source code is available on GitHub.
The author aims to explore C++26 progressively, focusing on its new features, particularly reflection. C++26 introduces the `enumerators_of()` function, allowing easy access to enum class members without manual updates. The author discusses utilizing this feature to convert enum values to strings and promises to share further insights as exploration continues.
The author explores the new "template for" feature in C++26 while attempting to reduce redundancy in calling a function template. They illustrate the concept with examples, highlighting how "template for" allows iterating over arrays as non-type template arguments. The discussion includes compiling the code and best practices for defining arrays.
The author successfully installed the GCC 16.1 compiler on an Ubuntu machine, outlining the installation steps and necessary dependencies. Following installation, a sample C++26 program utilizing reflection was compiled and executed without issues, demonstrating the new features of the compiler. The process and the author's excitement for the new capabilities are detailed.