Variable number of arguments in C++? In this post you will find a way to write variable arguments for template,
template <typename T>
void func(T t)
{
std::cout << t << std::endl ;
}
template<typename T, typename... Args>
void func(T t, Args... args) // recursive variadic function
{
std::cout << t <<std::endl ;
func(args...) ;
}
But in my case, type name is certain,
template <int... args>
constexpr auto sum(int a, ...) {
return a + sum(args...);
}
constexpr auto sum(int a, int b) {
return a + b;
}
void test() {
cout << sum(1, 2) << endl;
cout << sum(1, 2, 3) << endl;
cout << sum(1, 2, 3, 4) << endl;
}
It's not working with sum(args...)
, prompts error
Error C2660 '__syntaxTest::sum': function does not take 0 arguments ForTest
Do you have any suggestion about this?