I would write the function like this:
int fun(int a, int b, int c, int d) {
if (a) return a;
else if (b) return b;
else if (c) return c;
return d;
}
It is clean and short. I could stop here, but lets explore what can be done...
There exists an algorithm that already almost does what you want. A slight modification of the solution in this answer:
#include <algorithm>
#include <initializer_list>
template <typename T>
T first_non_zero_or_zero(const std::initializer_list<T>& args)
{
auto it = std::find_if_not(args.begin(),args.end(),[](auto v){ return v==0;});
return (it != args.end()) ? *it : 0;
}
The drawback of using a function for boolean expressions is no short-cuirciting. If you call the function via:
auto x = first_non_zero_or_zero( { foo(), expensive_function() });
Then expensive_function
must be called, no matter what foo
returns. The way to restore the ability to short-circuit is to pass callables instead, that would be
template <typename F>
auto first_non_zero_or_zero(F f){ return f();}
template <typename First,typename...F>
auto first_non_zero_or_zero(First f,F... others){
if (auto temp = f()) return temp;
return first_non_zero_or_zero(others...);
}
int foo(){ return 0;}
int expensive_function(){ return 42;}
int main()
{
std::cout << first_non_zero_or_zero(foo,expensive_function);
return 0;
}
However, this will make calls unnecessarily verbose when called with simple int
s, as you need to wrap them in a callable:
int fun(int a,int b,int c) {
first_non_zero( [](){ return a;},
[](){ return b;},
[](){ return c;})
}
Conclusion: Don't make things more complicated than necessary. Functions should do one thing. The one thing your fun
does is to return the first non-zero of 4 integers and a if-else
is the most simple way to get that done.