编译期求多个布尔值的与值
#include <iostream>
constexpr auto all() { return true; }
template <typename Arg, typename... Rest> requires std::is_same_v<Arg, bool>
constexpr auto all(Arg arg, Rest... rest) { return arg && all(rest...); }
template <typename... Ts> requires (true && ... && std::is_same_v<Ts, bool>)
constexpr auto all_1(Ts... ts) { return (true && ... && ts); }
int main(int argc, char *argv[])
{
using std::cout;
using std::endl;
cout << std::boolalpha << all(true, true, true) << endl;
cout << std::boolalpha << all_1(true, false, true) << endl;
return 0;
}
使用constexpr关键字进行编译期计算,利用requires子句限定所有参数都为bool类型。