For your specific example, you could shorten it to something like this:
int countZeros(int a, int b, int c, int d)
{
return int(a == 0) + int(b == 0) + int(c == 0) + int(d == 0);
}
Live Demo
However, a variadic template function would be a better way to go, then you can pass in as many parameters as you want.
@cigien's answer shows you how to use fold expressions, which were added in C++17. But if you are using C++11 or C++14 instead, you can still use a variadic template function, you would just have to loop over the parameters manually (see How can I iterate over a packed variadic template argument list?), eg:
template <typename... Args>
int countZeros(Args... args)
{
int zeros = 0;
for(int i : {args...}) {
zeros += int(i == 0);
}
return zeros;
}
Live Demo
Alternatively:
#include <array>
#include <algorithm>
template <typename... Args>
int countZeros(Args... args)
{
std::array<int, sizeof...(args)> a = {args...};
return std::count_if(a.begin(), a.end(), [](int i){ return (i == 0); });
}
Live Demo
Alternatively:
// for when the template parameter pack is empty...
int doCountZeros() {
return 0;
}
template <typename Arg, typename... Args>
int doCountZeros(Arg firstArg, Args... otherArgs) {
return int(firstArg == 0) + doCountZeros(otherArgs...);
}
template <typename... Args>
int countZeros(Args... args) {
return doCountZeros(args...);
}
Live Demo
Any of these approaches will happily handle your example of countZeros(varA, varB, varC, varD)
.
Your example also shows the summing of the 4 variables together. That can be handled with a function as well, eg:
int sum(int a, int b, int c, int d)
{
return a + b + c + d;
}
Live Demo
Or better, via a variadic function. If you are not using C++17 fold expressions, you can loop through the parameters manually, similar to shown above (also see Add all parameters with parameter pack expansion), eg:
template <typename... Args>
int sum(Args... args)
{
int result = 0;
for(int i : {args...}) {
result += i;
}
return result;
}
Live Demo
Alternatively:
#include <array>
#include <numeric>
template <typename... Args>
int sum(Args... args)
{
std::array<int, sizeof...(args)> a = {args...};
return std::accumulate(a.begin(), a.end(), 0);
}
Live Demo
Alternatively:
int doSum() {
return 0;
}
template <typename Arg, typename... Args>
int doSum(Arg firstArg, Args... otherArgs) {
return firstArg + doSum(otherArgs...);
}
template <typename... Args>
int sum(Args... args) {
return doSum(args...);
}
Live Demo
Any of these approaches will let you use sum(varA, varB, varC, varD)
where needed.