My goal here is to pass the non-empty value of 2 given values (either a string or an array) to a function foo
.
In Javascript I'd be able to do:
// values of variables a and b when calling foo
// a = "hello"
// b = []
foo( a || b )
// passes a, since b is empty and a is not (i.e
// it contains at least 1 character)
In C++ however, this doesn't work since the logical operator ||
only works with boolean values.
I would like to know if there is a shorter alternative to:
std::vector<std::string> a; // assuming this is empty
std::string b; // assuming this is NOT empty (contains a string value)
if (a.empty()){
foo(b);
}else{
foo(a);
}