c++ - How to reduce Trait-values of variadic template parameter to one value? -
while experimenting variadic templates came point useful make kind of reduction of trait values 1 final value via operation. use-case me is:
constexpr bool and(bool lhs, bool rhs){return lhs && rhs;} struct foo { template< typename ...ts> foo( ts&&... args) noexcept(traitreduction<std::is_nothrow_move_constructible, and, ts...>::value) {/*...*/} } the problem stl traits single template typed. current working solution is:
template< template<typename> class traitt, bool (*operator)(bool,bool), typename t1, typename ...ts> struct traitreduction { static bool const value = traitt<t1>::value; }; template< template<typename> class traitt, bool (*operator)(bool,bool), typename t1, typename t2, typename ...ts> struct traitreduction< traitt, operator, t1, t2, ts...> { static bool const value = (*operator)( traitt<t1>::value, traitreduction<traitt, operator, t2, ts...>::value); }; my question if stl gives standardized (probably more convenient) solutionfor task? , of course happy here comments on current solution, bad or better.
your solution linear in term of instantiations (and without advantage of short-circuit)
you may in less instantiations (and still without short-circuit)
template <bool...> struct bools{}; template <template <typename> trait, typename ... ts> struct all_of : std::is_same<bools<true, trait<ts>::value...>, bools<trait<ts>::value..., true>> {}; you may use std::conjunction linear, short-circuit.
c++1z , folding expression has nice syntax, has less instantiations (but without short-circuit (for instantiation)):
(trait<ts>::value && ...)
Comments
Post a Comment