From 5aa2cd5eb9292cc2354267063e9170c1cbb91ec5 Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Thu, 8 Sep 2016 17:45:53 -0400 Subject: [PATCH] Template simplifications Switch count_t to use constexpr_sum (under non-MSVC), and then make all_of_t/any_of_t use it instead of doing the sum itself. For MSVC, count_t is still done using template recursion, but all_of_t/any_of_t can also make use of it. --- include/pybind11/common.h | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/include/pybind11/common.h b/include/pybind11/common.h index c086e8796..255cc79a3 100644 --- a/include/pybind11/common.h +++ b/include/pybind11/common.h @@ -342,22 +342,24 @@ constexpr size_t constexpr_sum() { return 0; } template constexpr size_t constexpr_sum(T n, Ts... ns) { return size_t{n} + constexpr_sum(ns...); } -/// Return true if all/any Ts satify Predicate +// Counts the number of types in the template parameter pack matching the predicate #if !defined(_MSC_VER) template class Predicate, typename... Ts> -using all_of_t = bool_constant<(constexpr_sum(Predicate::value...) == sizeof...(Ts))>; -template class Predicate, typename... Ts> -using any_of_t = bool_constant<(constexpr_sum(Predicate::value...) > 0)>; +using count_t = std::integral_constant::value...)>; #else // MSVC workaround (2015 Update 3 has issues with some member type aliases and constexpr) -template class P, typename...> struct all_of_t : std::true_type { }; -template class P, typename T, typename... Ts> -struct all_of_t : conditional_t::value, all_of_t, std::false_type> { }; -template class P, typename...> struct any_of_t : std::false_type { }; -template class P, typename T, typename... Ts> -struct any_of_t : conditional_t::value, std::true_type, any_of_t> { }; +template class Predicate, typename... Ts> struct count_t; +template class Predicate> struct count_t : std::integral_constant {}; +template class Predicate, class T, class... Ts> +struct count_t : std::integral_constant::value + count_t::value> {}; #endif +/// Return true if all/any Ts satify Predicate +template class Predicate, typename... Ts> +using all_of_t = bool_constant<(count_t::value == sizeof...(Ts))>; +template class Predicate, typename... Ts> +using any_of_t = bool_constant<(count_t::value > 0)>; + // Extracts the first type from the template parameter pack matching the predicate, or Default if none match. template class Predicate, class Default, class... Ts> struct first_of; template class Predicate, class Default> struct first_of { @@ -373,13 +375,6 @@ struct first_of { }; template class Predicate, class Default, class... T> using first_of_t = typename first_of::type; -// Counts the number of types in the template parameter pack matching the predicate -template class Predicate, typename... Ts> struct count_t; -template class Predicate> struct count_t : std::integral_constant {}; -template class Predicate, class T, class... Ts> -struct count_t : std::integral_constant::value + count_t::value> {}; - /// Defer the evaluation of type T until types Us are instantiated template struct deferred_type { using type = T; }; template using deferred_t = typename deferred_type::type;