Empty Parameter Packs in Fold Expressions

How do fold expressions handle empty parameter packs?

Fold expressions handle empty parameter packs differently depending on whether they are unary folds or binary folds. Here's how empty parameter packs are treated in each case:

Unary Folds

Unary folds expect the parameter pack to have at least one element. If the parameter pack is empty, it results in a compilation error.

For example, in this case, calling Product() with an empty parameter pack triggers a compilation error because a unary fold over the * operator requires a non-empty parameter pack.

To handle empty parameter packs with unary folds, you need to provide an explicit base case or use a binary fold instead.

template <typename... Types>
auto Product(Types... Args) {
  return (... * Args);  
}

int main() {
  // Compilation error
  Product(); 
}
error: a unary fold expression over '*' must have a non-empty expansion

Binary Folds

Binary folds handle empty parameter packs by using the identity value of the operator as the result of the fold expression. For example:In this case, calling Sum() with an empty parameter pack results in the identity value of the + operator, which is 0.

#include <iostream>

template <typename... Types>
auto Sum(Types... Args) {
  return (0 + ... + Args);  
}

int main() {
  int EmptySum = Sum();
  std::cout << EmptySum;
}
0

The binary fold expression (0 + ... + Args) evaluates to 0 when Args is empty.

The identity value depends on the operator used in the binary fold

  • For addition (+), the identity value is 0.
  • For multiplication (*), the identity value is 1.
  • For logical AND (&&), the identity value is true.
  • For logical OR (||), the identity value is false.
  • For comma (,), the identity value is void().

To ensure that your fold expressions handle empty parameter packs correctly, you can:

  • Use binary folds with an appropriate identity value when you need to support empty parameter packs.
  • Provide an explicit base case or specialization for empty parameter packs if using unary folds.
  • Be aware of the operators that have an identity value and choose them appropriately based on your requirements.

By understanding how fold expressions handle empty parameter packs, you can write more robust and flexible code that works correctly in both empty and non-empty scenarios.

Fold Expression

An introduction to C++17 fold expressions, which allow us to work more efficiently with parameter packs

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Fold Expressions with Different Types
Can fold expressions be used with parameter packs containing different types?
Capturing Parameter Packs in Lambdas
How can I capture a parameter pack in a lambda expression within a fold expression?
Fold Expressions vs Variadic Templates
What are the advantages of using fold expressions over traditional variadic template techniques?
Fold Expressions with Side Effects
Are there any caveats to be aware of when using fold expressions with operations that have side effects?
Fold Expressions and Constexpr
Can fold expressions be used in constexpr functions?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant