Can you use C++11 variadic templates to complete /* ??? */
in:
template<bool...v> struct var_and { static bool constexpr value = /* ??? */; };
so that var_and<v...>::value
provides &&
over the boolean pack v
at compile-time?
Can you do the same for struct var_or<v...>
for ||
?
Can you use short-circuit evaluation (in both cases)?
Edit: An update to the accepted answer added that C++17 fold expressions enable
template<bool... v> constexpr bool var_and = (v && ...);
template<bool... v> constexpr bool var_or = (v || ...);
It seems that, for parameter pack-based approaches, only a restricted type of "short-circuit evaluation" is possible: while instantiating var_or<true,foo(),bar()>
only calls ||
once, it also calls both foo
and bar
.