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
3
votes
2 answers

lambda fold expression get index

Is there a simple way to add the current index of the arg As in include it in the lambda call so I don't need to use index++ template void Function(Args... args) { std::size_t size = sizeof...(Args); std::size_t index =…
3
votes
1 answer

How to expand into initializer list with fold expressions?

I would like to insert as many zeros into a vector as there are arguments to a variadic templated function (i.e. number of arguments zeros into a vector). I am trying to use fold expressions to achieve this and it works when using…
j00hi
  • 5,420
  • 3
  • 45
  • 82
3
votes
1 answer

Apply function to tuple of vectors to get tuple of elements (variadic templates)

I have a container class which takes a list of vector or array objects (and stores a tuple of references to the original list of objects). Now, when calling the container via operator()(size_t idx) I want the function to return a tuple that contains…
Phil-ZXX
  • 2,359
  • 2
  • 28
  • 40
3
votes
3 answers

Fold expression vs compile recursion

In c++17 we have fold expression which can greatly simplify code that could otherwise be implemented using compiler-recursion and SFINAE or overloading. For example, in the following code #include #include template
francesco
  • 7,189
  • 7
  • 22
  • 49
3
votes
1 answer

Fold expression within assertion compiles on some machines, but not on others

I have the following code that is (for demonstration purposes) supposed to assert that all arguments evaluate to true using C++17 fold expressions. #include template void fn(Ts... ts) { assert(ts && ...); } int…
Post Self
  • 1,471
  • 2
  • 14
  • 34
3
votes
0 answers

declaration of void variable shadows template parameter in fold expression

Is the following g++'s behaviour a bug? #include #include template< std::size_t ...i > constexpr bool f(int k, std::index_sequence< i... >) { int j = (std::size_t((void(i), 1)) + ...) * k; return 0 <…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
3
votes
0 answers

A C++17 parameter pack compiling error

I've got a program from internet like below, to show the ability of "parameter pack" in C++17 #include #include #include #include #include #include template void…
Troskyvs
  • 7,537
  • 7
  • 47
  • 115
3
votes
1 answer

GCC unpacks (I < ...) fold expression the wrong way

I've opened a bug to GCC, but I'd like to know if I'm right in my expectations. Consider this proposal and the following fold expression: (args < ...) It should be equivalent to: ((args$0 < args$1) < ...) < args$n Consider the following…
skypjack
  • 49,335
  • 19
  • 95
  • 187
2
votes
1 answer

How to create an array of N floats values with fold expression?

Suppose the following function template constexpr std::array make_ones() { std::array ret{}; for (size_t k = 0; k != N; ++k) { ret[k] = 1.0f; } return ret; } Is it possible to write that…
user877329
  • 6,717
  • 8
  • 46
  • 88
2
votes
0 answers

Which C++ fold idiom is preferred, short circuit comma or nested lambda?

First option technically has less cognitive load because you don't need to be concerned with || or , operators. It is unfortunate that two nested lambdas are required to first get Is, and then to fold on the inner lambda. See cppreference fold…
okovko
  • 1,851
  • 14
  • 27
2
votes
2 answers

Can you achieve fn(x1 ^ fn(x0)) with a fold expression?

Is it possible to achieve the following using a fold expression? template auto foo(Args... args) { //calling foo(x0, x1, x2) should be exactly equivalent to //calling fn(x2 ^ fn(x1 ^ fn(x0))) }
Chris_F
  • 4,991
  • 5
  • 33
  • 63
2
votes
2 answers

use template fold expressions instead of recursive instantions of a function template?

Given a variadic parameter pack, I would like to call a function with each element and its index in the pack. I have a simple example below which uses recursive instantiations of a function template to achieve this. (on godbolt) #include…
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
2
votes
1 answer

Template with fold expression creates unwanted parameter copy

The following prototype is intended to make synchronized print: #include #include #include #include #include // for OutputDebugString std::mutex sync_mutex; template void…
Alex F
  • 42,307
  • 41
  • 144
  • 212
2
votes
2 answers

Short circuiting in fold expressions

This is a self triggered question based on a self-answer I gave here. This seems a pretty convincing explanation of why short-circuiting of logical operators is available in fold expressions, and of the fact that wrapping a fold expression in a…
Enlico
  • 23,259
  • 6
  • 48
  • 102
2
votes
1 answer

Conditionally remove function calls in fold expression

I know that I can use SFINAE to disable generation of templated functions based on a condition, but that doesn't really work in this case. I want to initialize an array at compile-time that should contain values that matches a condition. Something…
Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50