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
5
votes
1 answer
How to use Folding Expression to initialize an array?
I come across with a weird problem in which MSVC doesn't let me to use fold expression to initialize an array in the following:
#include
template
class Matrix {
public:
void print()
{
…

Nima Ghorab
- 309
- 1
- 3
- 13
5
votes
2 answers
non type template parameter pack expansion
I simply would print out a non type template parameter pack. But I can't find a valid expansion if I want to have spaces in between the elements.
Example:
template < int ... N >
struct X
{
static void Check() {
// this will become…

Klaus
- 24,205
- 7
- 58
- 113
5
votes
1 answer
Why is the use of std::min in these fold expressions undefined behavior?
Thanks to Can I implement max(A, max(B, max(C, D))) using fold expressions? I'm aware of one working approach to use std::min in a fold expression(min2 below). However, I'm curious why the below approaches min1 and min3 are considered undefined…

tangy
- 3,056
- 2
- 25
- 42
5
votes
1 answer
Can I std::forward the arguments in fold expression?
In C++11 we have variadic templates, in which we can std::forward the arguments like in the following code
#include
#include
#include
void printVariadic()
{}
template
void…

LernerCpp
- 401
- 1
- 5
- 13
5
votes
3 answers
Is it possible to fold only part of the pack with C++17 fold expressions?
I'm trying to figure out how to fold only part of the variadic template pack with C++17 fold expressions.
Suppose I'd like to create a compile time "separated string" like "str1_str2_str3_....."
It can be pretty easily done with such a code (just an…

Dmitry
- 1,065
- 7
- 15
5
votes
2 answers
Enumerating over a fold expression
I have some auxiliary code that performs vector reshuffling using compile-time indices. It is of upmost importance that the generated code is as efficient as possible. I am relying on parameter packs with fold expressions, and I was wondering what…

MrMobster
- 1,851
- 16
- 25
5
votes
1 answer
Fold expressions with operator >>
Consider the following snippet:
#include
template void writeall(const types & ... items)
{
(std :: cout << ... << items);
}
template void readall(types & ... items)
{
(std :: cin >> ... >>…

Matteo Monti
- 8,362
- 19
- 68
- 114
5
votes
1 answer
How are fold expressions used in the partial ordering of constraints?
§14.10.3 Partial ordering by constraints [temp.constr.order] of N4553 specifies that constraint expressions formed of concepts and logical operators should be partially ordered and used to select the best viable function in cases of overloading. But…

Ryan Burn
- 2,126
- 1
- 14
- 35
4
votes
2 answers
How can I expand this expression to the return types of callables?
Why can't I expand the parameter types?
auto lambda = []() { return 'c'; };
template
struct MyClass
{
};
template
void createMyClass(Ts&& ... ts)
{
/* SUPPOSED TO CREATE MY CLASS WITH THE RETURN VALUES OF…

Zebrafish
- 11,682
- 3
- 43
- 119
4
votes
1 answer
Using fold expression to initialize static constexpr class data member doesn't compile
I am confused about a particular piece of code that won't compile even though very similar pieces of code do compile.
This will not compile:
#include
template
class Foo
{
static constexpr std::size_t BIT_COUNT =…

Jupiter
- 1,421
- 2
- 12
- 31
3
votes
2 answers
`if constexpr` in fold expression
I am trying to make a fold expression in a function which populates the outgoing parameters of a function with some values that come in from a string vector. My fold expression is like:
((if constexpr (std::is_integral_v)
{
args =…

Ferenc Deak
- 34,348
- 17
- 99
- 167
3
votes
4 answers
Test if elements are sorted with C++17 fold-expression
I am looking for a way to check that arguments are sorted using a c++ fold expression.
The naive approach:
template
bool are_strictly_increasing(Args const&... args) {
return (args < ...);
}
Does not work as it expands to…

gg99
- 445
- 6
- 12
3
votes
1 answer
How to stop fold expression function calls in the middle when a condition is met and return that value?
I have functions foo, bar, baz defined as follows:
template
int foo(T... t)
{
return (bar(t), ...);
}
template
int bar(T t)
{
// do something and return an int
}
bool baz(int i)
{
// do something and return…

locke14
- 1,335
- 3
- 15
- 36
3
votes
1 answer
Where is the fold expression ()?
Reference fluentCPP article
The below code explanation says that this structure inherits from several lambdas, can be constructed from those lambdas, and folds over the using expression.
template
struct overloaded : public…

Test
- 564
- 3
- 12
3
votes
1 answer
When are fold expressions as template parameters allowed?
I have the following code:
#include
// ok
template && ...), int> = 0>
void foo(Args ...args);
template
struct Type {
// syntax error
template…

Jan Schultke
- 17,446
- 6
- 47
- 96