unknown.prince
unknown.prince

Reputation: 752

fold expression in assignment

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 ';'

Upvotes: 2

Views: 1406

Answers (1)

Barry
Barry

Reputation: 303537

Fold expressions have to be parenthesized. Hence:

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

The inner parentheses are necessary as well.

Upvotes: 10

Related Questions