fold expressions, since C++17, are used to reduce the parameter packs of variadic templates over a binary operator.
Questions tagged [fold-expression]
144 questions
37
votes
1 answer
Is there a reason to use std::conjunction/std::disjunction instead of a fold expression over "&&"/"||"?
Is there any specific cases you cannot correctly do with std::conjunction/std::disjunction and not using the more "fundamental" (i.e. language feature instead of library feature) fold expression over &&/||?
Example:
// func is enabled if all Ts...…

rubenvb
- 74,642
- 33
- 187
- 332
29
votes
9 answers
Can I implement max(A, max(B, max(C, D))) using fold expressions?
While trying to play around with C++17 fold expressions, I tried to implement max sizeof where result is maximum of the sizeof of types.
I have an ugly fold version that uses variable and a lambda, but I am unable to think of a way to use fold…

NoSenseEtAl
- 28,205
- 28
- 128
- 277
23
votes
2 answers
How do I restrict fold expressions with C++ 20 requirements/concepts?
How do you restrict the allowed types in variadic templates and fold expression using C++20 concepts?
For example suppose I'd like to restrict the following fold expression to only support integral types, how would I do that?
#include…

Gonen I
- 5,576
- 1
- 29
- 60
19
votes
1 answer
Is it legal to use an operator in a fold expression that was introduced by a using declaration?
I was experimenting with using arbitrary functions in fold expressions, when I found the following code that compiles with gcc but does not compile with clang.
enum Enum {
A = 3,
B = 8,
C = 5
};
namespace EnumMax {
constexpr Enum…

Mark
- 1,016
- 6
- 10
18
votes
1 answer
pointer to member function in fold expression
This code calls &foo::next multiple times
struct foo
{
foo& next()
{
return *this;
}
};
template
void tmp_funct_1(Obj& obj)
{}
template
void tmp_funct_1(Obj& obj,…
user9681493
16
votes
2 answers
Syntax issue when populating an array with a fold expression
Yes, I can use std::initializer_list. Yes, even easier, I can do aggregate initialization. But how does this work? I can't seem to fold my head around C++17's fold expressions. There aren't enough examples out there.
Here's what I came up…

DeiDei
- 10,205
- 6
- 55
- 80
15
votes
3 answers
Folding expressions in C++17 - Usecase for comparison operators
According to N4295 C++17 will allow me to calculate the sum of an unknown number of arguments thus:
template
int sum(T...t)
{
return (... + t);
}
The document further states that I could use operators such as == or > instead of…

oz1cz
- 5,504
- 6
- 38
- 58
14
votes
1 answer
Can spaceship operator be used in fold expressions?
None of the compilers I tried accept such code:
template bool foo() { return (a<=> ... <=>0); }
But for any other <=,>=,==,!=,<,> it compiles.
cppreference is clear here - there is no <=> on the list of binary operators we can use for…

PiotrNycz
- 23,099
- 7
- 66
- 112
14
votes
5 answers
How to make grouped or paired fold of parameter pack?
template
std::wstring descf(Msg, Args&&... args) {
std::wostringstream woss;
owss << Msg << ". " << ... << " " << args << ": '" << args << "' ";//not legal at all
//or
owss << Msg << ". " << args[0] << ":…

darune
- 10,480
- 2
- 24
- 62
14
votes
3 answers
Clang and the binary fold expressions — The curse of the empty parameter pack
Specifically Clang 3.6.0, the one currently hosted by Coliru.
All these snippets are called from :
int main() {
foo();
std::cout << "\n----\n";
foo(1, 2, 3);
}
The following code :
template
void foo(Args... args) {
…

Quentin
- 62,093
- 7
- 131
- 191
13
votes
2 answers
Associativity of fold-expressions
N4191 proposed fold-expressions to C++. The definition there was that
(args + ...)
is a left-fold (i.e. (((a0 + a1) + a2) + ...), and that
(... + args)
is a right-fold (i.e. (... + (a8 + (a9 + a10))). However, the revised paper N4295 reversed…

TemplateRex
- 69,038
- 19
- 164
- 304
12
votes
3 answers
Test if all elements are equal with C++17 fold-expression
I have a function taking a variadic parameter pack and at the beginning I want to check that all elements compare equal. Can I somehow use the new C++17 fold-expressions to write that succinctly as a one-liner? I was thinking
template

levzettelin
- 2,600
- 19
- 32
12
votes
1 answer
"Uninitialized captured reference" error when using lambdas in fold expression - clang vs gcc
Consider the following code:
template
auto fold_left(F&& f, X0&& x0, X1&& x1, Xs&&... xs)
{
auto acc = f(x0, x1);
return ([&](auto y){ return acc = f(acc, y); }(xs), ...);
}
const…

Vittorio Romeo
- 90,666
- 33
- 258
- 416
12
votes
2 answers
Using fold expressions to print all variadic arguments with newlines inbetween
The classic example for C++17 fold expressions is printing all arguments:
template
void print(Args ... args)
{
(cout << ... << args);
}
Example:
print("Hello", 12, 234.3, complex{12.3f,…

Thomas McGuire
- 5,308
- 26
- 45
12
votes
1 answer
Fold expressions and empty parameters pack: what's the expected result?
Consider the following minimal example:
#include
template
constexpr auto sum() { return (I + ...); }
template
constexpr auto check() { return (B && ...); }
int main() {
static_assert(6 == sum<1,2,3>(),…

skypjack
- 49,335
- 19
- 95
- 187