variant


1. Store Different Data Types in a Single Container

#include <variant>

struct Point { double x, y; };
using Circle = std::variant<Point, double>;

int main() {
    Circle c1 = Point{1.0, 2.0};
    Circle c2 = 10.0;
}

2. Handle Null Pointers

#include <variant>

using OptionalInt = std::optional<int>;

int main() {
    OptionalInt oi; // Initialized as null
    oi = 10; // Set a value
    if (oi) {
        // Handle the value
    } else {
        // Handle null
    }
}

3. Represent a Union of Classes

4. Reduce Branching

5. Implement Type Erasure

6. Store a Set of Values

7. Implement an Enum with Associated Data

8. Store a Range of Values

9. Represent a Heterogeneous Collection

10. Detect Type at Runtime

11. Combine Variants

12. Check for Null Values

13. Extract Values

14. Iterate Over Variants

15. Create Variadic Variants

16. Compare Variants

17. Store a Pointer to an Object

18. Implement a Polymorphic Function

19. Handle Unknown Types

20. Store a Function

21. Implement a Customizable Data Structure

22. Represent a JSON Object

23. Represent a Binary Tree

24. Implement a Type-Safe Union

25. Represent a Finite State Machine

26. Store a Lazy-Evaluated Value

27. Implement a Variant-Based Queue

28. Represent a Variant of Containers

29. Implement a Visitor Pattern

30. Represent a Function with Multiple Signatures

31. Store a Polymorphic Reference

32. Implement a Type-Erased Class

33. Store a Range of Values with Step

34. Represent a Sparse Array

35. Implement a Type-Safe Union with Default Value

36. Store a Function with Bound Arguments

37. Implement a Type-Erased Tuple

38. Represent a Union of References

39. Store a Variant of Optionals

40. Implement a Type-Safe Union with Default Construction

41. Represent a Union of Unique Pointers

42. Store a Variant of Pairs

43. Implement a Type-Safe Union with Custom Construction

44. Represent a Union of Weak Pointers

45. Store a Variant of Arrays

46. Implement a Type-Safe Union with Multiple Default Values

47. Represent a Union of Shared Pointers

48. Store a Variant of Structs

49. Implement a Type-Safe Union with Default Value and Custom Construction

50. Represent a Union of Class Instances