Compile-Time Magic: C++23’s std::expected and the Limits of Compile-Time Computation

Today, let’s talk about a “black magic” tool brought by C++23—std::expected. It is not just an upgraded version of error handling; it can perfectly combine with compile-time computation (constexpr), allowing you to handle “expected values” or “unexpected errors” at the compilation stage. Imagine calculating complex expressions during code compilation, returning error messages directly if something … Read more

A New Era of Type Safety in C++: In-Depth Analysis of std::optional, std::variant, std::expected, and Herbception

A New Era of Type Safety in C++: In-Depth Analysis of std::optional, std::variant, std::expected, and Herbception

🔍Introduction: The Evolution from Wild Pointers to Type Safety C++17/23 introduced std::optional, std::variant, std::expected, and Herb Sutter‘s Herbception, which form the three pillars of modern C++ type safety. This article provides a comprehensive analysis of their differences and practical scenarios through illustrations+code, helping you write more robust code! 🛡️ 1. std::optional: An Elegant Expression of … Read more