jeudi 21 juillet 2016

How to pass variadic parameters through functions in C++ 11?

I am trying to understand variadic function calls in C++, in particular parameter packs. Consider this example (live example here):

int sum() { return 0; }
int sum(int x, int... rest) {
  return x + sum(rest...);
}

int pass_through(int... args) {
  return sum(args...);
}

int main(int argc, char *argv[]) {
  std::cout << pass_through(1,2,3,4,5) << "\n";
  return 0;
}

I would like to pass a number of values to the bounce function which simply passes them to foo. The foo function then uses the parameters one by one to compute a result.

However, above code results in a number of different errors, like error: expansion pattern 'int' contains no argument packs and error: 'rest' was not declared in this scope.

What am I missing here?

Aucun commentaire:

Enregistrer un commentaire