YSC*_*YSC 1 c++ standard-library c++17
我已经看到了std::disjunction标准库(源代码)中的实现:
template<class...> struct disjunction : std::false_type { };
template<class B1> struct disjunction<B1> : B1 { };
template<class B1, class... Bn>
struct disjunction<B1, Bn...>
: std::conditional_t<bool(B1::value), B1, disjunction<Bn...>> { };
Run Code Online (Sandbox Code Playgroud)
我很好奇,需要专门disjunction<B1>为B1.为什么它比我天真的实施更好?
template<class...> struct or_t
: std::false_type {};
template<class B1, class... Bn> struct or_t<B1, Bn...>
: std::integral_constant<bool, bool(B1::value) || bool(or_t<Bn...>::value)> {};
Run Code Online (Sandbox Code Playgroud)