8

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 <typename Res, typename... Ts>
    vector<Res> to_vector(Ts&&... ts) {
        vector<Res> vec;
        (vec.push_back(ts) ...); // *
        return vec;
    }

What is the value of "pack", "op" and "init" in line *, if any?

This example is from page 244 of Bjarne Stroustrup's A Tour of C++ book, and seems like a comma was forgotten in the example, hence my confusion.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
happy_sisyphus
  • 1,693
  • 1
  • 18
  • 27
  • you end up with bunch of `vec.push_back(...)`s separated by comma? – C.M. Sep 03 '19 at 18:50
  • Yes, that I understand. I want to know how the line fits into the definition of a valid fold expression. – happy_sisyphus Sep 03 '19 at 18:51
  • 4
    You can check [this errata](http://www.stroustrup.com/tour2_errata.html), then you will see that this book has many other errors, why not report this as typo - `,` is missing ? – rafix07 Sep 03 '19 at 19:05
  • @rafix07 I wouldn't call 10 errors in about 200 pages "many" – bolov Sep 04 '19 at 07:48

1 Answers1

12

The syntax is not valid. It's missing a comma (most likely a typo):

(vec.push_back(ts), ...)
//                ^

And so it is "unary right fold":

( pack op ... )

with op being a comma.

bolov
  • 72,283
  • 15
  • 145
  • 224
  • 1
    Note that when expanding a pack while calling a function, `(pack...)` is right, when folding `(pack,...)` is right. I get burned by this pretty often myself. – Yakk - Adam Nevraumont Jan 25 '21 at 16:43