Can I create a function which takes any number of arguments of the same type?

Use C++17 fold expression:

template<class... Args>
constexpr auto total_sum(const Args&... args) {
  return (args + ... + 0);
}

static_assert(total_sum(1, 2, 5, 4, 2) == 14);
static_assert(total_sum(3, 5, 6, 2) == 16);

Leave a Comment