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

From C’s Union to C++’s Variant: A Marvelous Journey of Type Safety Evolution

From C's Union to C++'s Variant: A Marvelous Journey of Type Safety Evolution

In the world of programming, we are always looking for safer and more elegant ways to express complex data structures. Today, let’s explore the union in C and the std::variant introduced in C++17, examining how they address similar problems in different eras, yet are fundamentally different. 🌟 Introduction: Why Do We Need Union Types? Imagine … Read more