Skip to content

Commit 356bf94

Browse files
authored
Merge pull request #401 from jagerman/templates-simplification
Small template simplifications
2 parents 260b26b + 5aa2cd5 commit 356bf94

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

include/pybind11/common.h

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -342,22 +342,24 @@ constexpr size_t constexpr_sum() { return 0; }
342342
template <typename T, typename... Ts>
343343
constexpr size_t constexpr_sum(T n, Ts... ns) { return size_t{n} + constexpr_sum(ns...); }
344344

345-
/// Return true if all/any Ts satify Predicate<T>
345+
// Counts the number of types in the template parameter pack matching the predicate
346346
#if !defined(_MSC_VER)
347347
template <template<typename> class Predicate, typename... Ts>
348-
using all_of_t = bool_constant<(constexpr_sum(Predicate<Ts>::value...) == sizeof...(Ts))>;
349-
template <template<typename> class Predicate, typename... Ts>
350-
using any_of_t = bool_constant<(constexpr_sum(Predicate<Ts>::value...) > 0)>;
348+
using count_t = std::integral_constant<size_t, constexpr_sum(Predicate<Ts>::value...)>;
351349
#else
352350
// MSVC workaround (2015 Update 3 has issues with some member type aliases and constexpr)
353-
template <template<typename> class P, typename...> struct all_of_t : std::true_type { };
354-
template <template<typename> class P, typename T, typename... Ts>
355-
struct all_of_t<P, T, Ts...> : conditional_t<P<T>::value, all_of_t<P, Ts...>, std::false_type> { };
356-
template <template<typename> class P, typename...> struct any_of_t : std::false_type { };
357-
template <template<typename> class P, typename T, typename... Ts>
358-
struct any_of_t<P, T, Ts...> : conditional_t<P<T>::value, std::true_type, any_of_t<P, Ts...>> { };
351+
template <template<typename> class Predicate, typename... Ts> struct count_t;
352+
template <template<typename> class Predicate> struct count_t<Predicate> : std::integral_constant<size_t, 0> {};
353+
template <template<typename> class Predicate, class T, class... Ts>
354+
struct count_t<Predicate, T, Ts...> : std::integral_constant<size_t, Predicate<T>::value + count_t<Predicate, Ts...>::value> {};
359355
#endif
360356

357+
/// Return true if all/any Ts satify Predicate<T>
358+
template <template<typename> class Predicate, typename... Ts>
359+
using all_of_t = bool_constant<(count_t<Predicate, Ts...>::value == sizeof...(Ts))>;
360+
template <template<typename> class Predicate, typename... Ts>
361+
using any_of_t = bool_constant<(count_t<Predicate, Ts...>::value > 0)>;
362+
361363
// Extracts the first type from the template parameter pack matching the predicate, or Default if none match.
362364
template <template<class> class Predicate, class Default, class... Ts> struct first_of;
363365
template <template<class> class Predicate, class Default> struct first_of<Predicate, Default> {
@@ -373,13 +375,6 @@ struct first_of<Predicate, Default, T, Ts...> {
373375
};
374376
template <template<class> class Predicate, class Default, class... T> using first_of_t = typename first_of<Predicate, Default, T...>::type;
375377

376-
// Counts the number of types in the template parameter pack matching the predicate
377-
template <template<typename> class Predicate, typename... Ts> struct count_t;
378-
template <template<typename> class Predicate> struct count_t<Predicate> : std::integral_constant<size_t, 0> {};
379-
template <template<typename> class Predicate, class T, class... Ts>
380-
struct count_t<Predicate, T, Ts...> : std::integral_constant<size_t,
381-
Predicate<T>::value + count_t<Predicate, Ts...>::value> {};
382-
383378
/// Defer the evaluation of type T until types Us are instantiated
384379
template <typename T, typename... /*Us*/> struct deferred_type { using type = T; };
385380
template <typename T, typename... Us> using deferred_t = typename deferred_type<T, Us...>::type;

0 commit comments

Comments
 (0)