Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 749
jaganantharjun Avatar asked Aug 12 '26 17:08

jaganantharjun


1 Answers

Fold expressions have to be parenthesized. Hence:

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

The inner parentheses are necessary as well.

like image 99
Barry Avatar answered Aug 14 '26 08:08

Barry