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
1
vote
3 answers

Iterate over 2 template parameter packs in parallel

I started to implement a very flexible Odometer. It may have several disks with even different amount of values on each different disk. And, as an extension, even the data type of values on each of the single disks could be different. All this shall…
A M
  • 14,694
  • 5
  • 19
  • 44
1
vote
1 answer

How to defer expanding a parameter pack?

I was toying around with tuples. I wanted to zip an arbitrary number of tuples. There is probably a better way to do that than what I came up with, but my solution led me to a problem that is interesting in itself. Sometimes, you want to expand one…
1
vote
1 answer

Fold expresion & CRTP

I'm writing this small application trying to test about fold expressions and i can't get it to compile, it complaints about ambiguous request on method Play, i don't understand why the signature of the function Play should be different on both…
jsubi
  • 9
  • 4
1
vote
2 answers

How can I assign element-wise to a tuple using fold expressions?

I have a type effectively wrapping a variadic std::tuple like this: #include #include template struct Foo { std::tuple t; Foo(Args&&... a) : t{ std::forward(a)... } { } …
Jodocus
  • 7,493
  • 1
  • 29
  • 45
1
vote
1 answer

variadic template function (to delete number of dynamically allocated variables)

I don't get exactly if I am doing the right thing template auto dealloc_all(AllVArgs &..._AllVArgs) -> void { (((std::cout << "\nin dealloc_all function " << &_AllVArgs), ...) << " "); ((delete _AllVArgs), ...); …
Gil Rovero
  • 35
  • 5
1
vote
2 answers

msvc and fold expression

I'm having trouble compiling this code on Windows. This code compile correctly on Linux, both with clang and gcc. I'm using msvc 19.29. Msvc exit with the not so helpful error C1001. struct Object{}; class Storage { Object &createObject() { …
dorfen_
  • 75
  • 1
  • 7
1
vote
1 answer

Why is the c++ filesystem directory_iterator a LegacyIterator?

According to the docs here this iterator is a LegacyInputIterator, which is a LegacyIterator. Now, according to my understanding of the meaning of 'legacy', it means this: In computing, a legacy system is an old method, technology, computer system,…
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
1
vote
2 answers

Does using postincrement within a fold-expression give unsequenced behaviour?

Recently I've came up with an idea how enumerate elements in a parameter pack (Pulses) and I've been happy with the solution for a while: lastValue = 0.0; i = -1; lastValue += (... + ((time <= endtimes[++i] && time >= delays[i]) ?…
Ranza
  • 294
  • 3
  • 10
1
vote
3 answers

How do I make these fold expressions work?

I'm not understanding why this is not working: template void func() { (std::cout << typeid(Types).name(), ...) ; // Syntax error, unexpected token '...', expected 'expression' (static_assert(std::is_integral_v,…
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
1
vote
3 answers

Improving a variadic template function using new c++14 / c++17 features

I am newbie to variadic templates, still I managed to program some code in c++11 using it, but I still feel sour about result because it lacks expressivity. The issue is to implement a function that accept several bool conditions (from 1 to…
Pablo
  • 557
  • 3
  • 16
1
vote
0 answers

C++17 Tree Traversal with Fold Expressions

I'm looking at the same tree traversal example as THIS POST. My VS is not supporting C++17 to see Fold Expresion inner workings. I guess this line: Node* node = traverse(root, left, right); would only traverse the binary tree from the root to the…
user2376997
  • 501
  • 6
  • 22
1
vote
1 answer

Pass a fold expression to a variadic template

consider the following function that prints the arguments using fold expressions. template void print(T &&... t) { (std::cout << ... << t) << '\n'; } Consider a second function that calls it : template
Midnight Exigent
  • 615
  • 4
  • 15
1
vote
0 answers

How did C++ compilers implemented fold-expressions in C++17?

I looked around the web to understand how did these highly simple-to-use expressions implemented behind the scenes, and I couldn't find anything that relates to the implementation of those expressions. For example: template struct…
Coral Kashri
  • 3,436
  • 2
  • 10
  • 22
1
vote
1 answer

Iterate over class inheritances in C++

Assume I have a some classes architecture (the number of the classes is growing up during the development time), that each class inherit from N classes with the same basic interface. What is the best way (if possible) to create a base function (in…
1
vote
1 answer

Non-recursively visit a custom variant - how to elegantly return a value?

I am writing a barebones version of std::variant for a personal project and learning experience. The visitation strategy that I want to implement is a if...else if chain, not a constexpr table of function pointers. The reason is that the latter is…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416