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.
You don’t want
valueto be a typedef.Obviously the same can be done for
||.Short circuit evaluation doesn’t matter because this only deals with constant expressions which won’t have any side effects.
Here’s another method which stops recursively generating types as soon as it find a false value, emulating a kind of short circuiting:
Update for C++17: Using a fold expression makes this much simpler.
Or also using a template variable as enobayram suggests: