Questions tagged [fold-expression]

fold expressions, since C++17, are used to reduce the parameter packs of variadic templates over a binary operator.

144 questions
11
votes
3 answers

Is it possible to insert extra operation in fold expression?

In C++17, fold expression is available, so to print arguments, we could use #define EOL '\n' template void output_argus(Args&&... args) { (cout << ... << args) << EOL; } int main() { output_argus(1, "test",…
r0n9
  • 2,505
  • 1
  • 29
  • 43
11
votes
2 answers

Checking if variadic template parameters are unique using fold expressions

Given a variadic template parameter pack, I want to check if all types given to it are unique using an inline constexpr bool and fold expressions. I trie something like this: template inline static constexpr bool is_unique = (... &&…
Joald
  • 1,114
  • 10
  • 32
11
votes
1 answer

C++17 fold expression in cout

I am learning the new c++17 fold expression and I saw this code from c++17 fold expression. I would like to know why this code work : template void printer(Args&&... args) { (std::cout << ... << args) << '\n'; } but not this…
10
votes
2 answers

Fold expressions in MSVC

I have the following function that computes the mean value: template auto mean_of(const Ts... values) { return (... + values) / static_cast(sizeof...(Ts)); } With VS 2017 15.6.0 Preview 3 the following code std::cout <<…
Evg
  • 25,259
  • 5
  • 41
  • 83
10
votes
2 answers

On what base fold expression of a parameter pack consisting of a single element is transformed into unparenthesized expression

Consider an example: #include template decltype (auto) foo(Ts... ts) { return (ts->x + ...); } struct X { int x; }; int main() { X x1{1}; …
W.F.
  • 13,888
  • 2
  • 34
  • 81
10
votes
2 answers

Can you use a subexpression within fold expressions?

Is the following a legal fold expression? template bool in_range(std::index_sequence) { return ((Ix < N) && ...); } It compiles with clang but not gcc
Ryan Burn
  • 2,126
  • 1
  • 14
  • 35
10
votes
1 answer

Why doesn't a left fold expression invert the output of a right fold expression?

I'm taking a look at C++17 fold expressions and I'm wondering why does the following program outputs 4 5 6 4 5 6 for both of the for_each calls template void for_each1(F fun, T&&... args) { (fun…
Marco A.
  • 43,032
  • 26
  • 132
  • 246
9
votes
3 answers

Expand parameter pack with index using a fold expression

I've got a template function taking a parameter pack. I want to expand it into calls to a second function while also supplying the index of the item in the pack. I probably can work out how to do it with recursion but I would like to try do it…
Joe
  • 5,394
  • 3
  • 23
  • 54
9
votes
3 answers

How to implement the generalized form of std::same_as (i.e. for more than two type parameters) that is agnostic to parameter order?

Background We know that the concept std::same_as is agnostic to order (in other words, symmetric): std::same_as is equivalent to std::same_as (related question). In this question, I would like to implement something more general:…
ph3rin
  • 4,426
  • 1
  • 18
  • 42
9
votes
1 answer

How to Insert spaces in (cout << ... << args) using fold expressions?

Given template void print(Types&& ...args) { (cout << ... << args); } // .... print(1, 2, 3, 4); // prints 1234 How to add spaces so we get 1 2 3 4? Update: Correct answer: ((std::cout << args << ' ') , ...);
Amin Roosta
  • 1,080
  • 1
  • 11
  • 26
9
votes
3 answers

Retrieve value out of "cascading ifs" fold expression

Let's assume that I want to create my own lambda-based switch with the following syntax: auto s = make_switch(std::pair{0, []{ return 0; }}, std::pair{1, []{ return 50; }}, std::pair{2, []{ return 100;…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
9
votes
1 answer

Order of Evaluation for Fold Expressions

Fold expressions seem to be a nice way to apply a function to each element of a tuple. However, if the applied function has side effects, the order of function invocations might be an important concern. Consider: #include…
ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
9
votes
1 answer

Are fold expressions a C++14 or a C++1z feature?

https://isocpp.org/std/the-standard states that the working draft N4296 contains the C++14 standard plus minor editorial changes. I noted that N4296 §5.1.3 contains a specification of "Fold expressions". I thought this to be a feature which did…
jotik
  • 17,044
  • 13
  • 58
  • 123
8
votes
1 answer

Why is this C++ fold expression valid?

On cppreference, I saw that there are four types of fold expressions, unary right, unary left, binary right, and binary left. What is the type of this fold expression here? I'm having a hard time understanding why it is valid. template
happy_sisyphus
  • 1,693
  • 1
  • 18
  • 27
8
votes
1 answer

Create a string with delimeters from fold expression

I've got the following function: template void Foo(Args && ... args) { std::stringstream stream; (stream << ... << args); // Do somethign with it } It can perfectly concat params into a string, but as expected it gives…
original.roland
  • 640
  • 5
  • 17
1
2
3
9 10