Fold Expressions
Explore fold expressions introduced in C++17 to efficiently process variadic template parameter packs using binary operators. Understand the syntax, variations, supported operators, and how fold expressions perform compile-time evaluation for more flexible and concise template code.
We'll cover the following...
We'll cover the following...
Fold Expressions (C++17)
Fold expressions is a nice syntax to evaluate binary operators at compile-time. Fold expressions reduce parameter packs on binary operators.
C++11 provides support for parameter packs:
C++17 provides support for fold expressions:
Two variations
- The fold expression either has or does not have an initial value
- The parameter pack will be processed from left or right
C++17 supports the following 32 operators in fold expressions:
+ - * / % ^ & | = < > << >> ...