Implementing variant Visitation Using Lambdas
summary
Demonstrates a concrete implementation of lambda-based visitation for std::variant and other variants: builds an overload set via multiple inheritance and using-declarations (works in C++14 via a recursive template), exposes match() and match_recursive() helpers, and handles recursive variants using forward declarations plus indirection (unique_ptr). Introduces a Y-Combinator for recursive lambdas to avoid std::function type-erasure and runtime cost; benchmarks show smaller assembly and no extra runtime overhead. Code compiles and runs, providing local, concise, exhaustive visitors for single and multiple dispatch.
AI-written summary · about
chapters (18)
- 0:00Introduction to variants
- 1:31Variants vs other types
- 4:22Traditional tagged unions
- 7:55Using std::variant
- 10:32Practical variant use cases
- 13:27Variant interface and access
- 17:31Introduction to visitation
- 20:32Traditional visitation methods
- 24:39Modern lambda visitation
- 29:18Implementing lambda overloads
- 34:40Advanced lambda overloading
- 43:47Implementing the match function
- 50:10Recursive variant structures
- 55:15Traditional recursive visitation
- 58:53Challenge of recursive lambdas
- 1:07:10The Y Combinator technique
- 1:15:13Recursive lambda visitation
- 1:27:47Conclusion and guidelines
related talks
43m“Variadic expansion in examples"
55m“Variants: Past, Present, and Future"
05mC++20 Lambdas: Familiar Template Syntax
55mCustom Overload Sets and Inline SFINAE for Truly Generic Interfaces
01h 04mVariadic Templates in C++11 / C++14 - An Introduction
58mC++17 in Practice - The good and the ugly
59mLambdas from First Principles: A Whirlwind Tour of C++""
51mSimple, Extensible Pattern Matching in C++14
