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

How to use if constexpr in template fold expressions?

I would like to write a sum function with variable number of argument with the condition that it should ignore argument that are not std::is_arithmetic I have figured out a recursive version that works auto old_sum(){ return…
8
votes
1 answer

Unpacking variadic tuples in c++17

Is there anything better in c++17 (maybe C++2a) than the classic C++14 way to unpack variadic tuple with std::index_sequence? Anything better than this: template class MultiIterator { public: MultiIterator(I const& ...i) …
PiotrNycz
  • 23,099
  • 7
  • 66
  • 112
8
votes
2 answers

Expanding parameter pack into lambda with fold expression - gcc vs clang

Considering the following code snippet: template void post(TF){ } template struct funcs : TFs... { funcs(TFs... fs) : TFs{fs}... { } void call() { (post([&]{ static_cast(*this)(); }),…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
7
votes
2 answers

Fold expression for a parameter pack with comma operator: How to add additional parameters when expanding the pack?

I want to design a compile-time-string class CTString that can e.g. be constructed from a parameter pack of string literals. This works using a comma-fold-expression (for this toy example, I tried to avoid the use of any system headers to make it…
7
votes
3 answers

What is a good alternative to this C++17 fold expression in C++14?

Here is a nice, succinct fold expression based lambda in C++17: #include using ::std::uint64_t; constexpr auto sumsquares = [](auto... n) { return ((n * n) + ...); }; // I want this to work. uint64_t foo(uint64_t x, uint64_t y, uint64_t…
Omnifarious
  • 54,333
  • 19
  • 131
  • 194
7
votes
3 answers

generating calls to lambdas with arbitrary number of parameters

The following definition has proven to be very useful for me: template void apply_on_each_args(Func f, Args... args) { (f(args), ...); } Basically, the arguments pack folded on comma operator, allows to define several…
7
votes
1 answer

How to make `short-circuit evaluation` also available in `fold expressions`?

#include #define FORWARD(arg)\ std::forward(arg) template constexpr bool AndL(Args&&... args) { return (... && FORWARD(args)); } template constexpr bool AndR(Args&&... args) { …
xmllmx
  • 39,765
  • 26
  • 162
  • 323
6
votes
4 answers

How to correctly unfold a pack in order to create N element array

I was expecting this code to create 10 elements vector each constructed as A{1, 2, 3.0}, but in reality the output is 1 #include #include struct A { int a{0}; int b{0}; double c{0.0}; }; template
Dmitry
  • 1,065
  • 7
  • 15
6
votes
2 answers

Copy-assigning to a tuple from an iterator range in C++17

I've been hoping to use new C++17 features like fold expressions and std::apply() to simplify some C++11 code I have that uses tools like std::index_sequence and std::initializer_list for some operations on tuples. One task in particular is giving…
jwimberley
  • 1,696
  • 11
  • 24
6
votes
1 answer

Using fold expression with std::apply on two tuples

I just started learning C++17 fold expressions. I understand that it is possible to apply a fold expression to a tuple, like in the following example (inspired by replies to this question): #include #include int main() { …
pmjobin
  • 813
  • 1
  • 7
  • 15
6
votes
5 answers

Print spaces between each element using a fold expression

I am using a fold expression to print elements in a variadic pack, but how do I get a space in between each element? Currently the output is "1 234", the desired output is "1 2 3 4" template > struct…
Lim Ta Sheng
  • 371
  • 3
  • 8
6
votes
3 answers

C++ unary right fold vs unary left fold with comma operator

Why comma separated unary left fold produce the same result as the right one? Live code template void right_fold(Args... args){ ((std::cout << args),...); } template void left_fold(Args... args){ …
tower120
  • 5,007
  • 6
  • 40
  • 88
6
votes
1 answer

Howto use a fold expression with a specific type?

I've a function to check if a std::string contains a substring. I pass the strings around as std::string_view, such that no copy takes place. bool containsSubstr(std::string_view str, std::string_view substr) { return str.find(substr) !=…
dani
  • 3,677
  • 4
  • 26
  • 60
6
votes
2 answers

C++17 Limit type of fold expression for initialization of a template-class

I basically try to write my own game engine for practice and personal use (I know, it's a nearly impossible task, but as I said, it's mostly for learning new things). Currently, I'm working on my math library (mainly vectors and matrices) and I came…
6
votes
1 answer

Implementing operator less for arrays using fold expressions

I am playing around with fold expression in c++17 with the latest clang++. I tried to implement the less operator for an array using this which I want to use for fixed size strings. Here is where I got to. Is there a better way to do this,…
zrb
  • 851
  • 7
  • 16
1 2
3
9 10