2

I am trying to use a fold expression to simplify some code. In following code, I am trying to insert elements into an array, but the fold expression does not compile

struct test {
  std::string cmd[20];
  test() {
    int i = 0;
    auto insert = [&](auto... c) {
      assert(i < 20);
      (cmd[i++] = c), ...;
    };
    insert("c");
    insert("c", "c2");
  }
};

compilers complains about missing ';'

Barry
  • 286,269
  • 29
  • 621
  • 977
unknown.prince
  • 710
  • 6
  • 19

1 Answers1

9

Fold expressions have to be parenthesized. Hence:

((cmd[i++] = c), ...);

The inner parentheses are necessary as well.

Barry
  • 286,269
  • 29
  • 621
  • 977