From 34d308adf05bd27659a86d0284d1060a43a7b0e9 Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Sun, 22 Jan 2017 19:12:24 -0500 Subject: [PATCH] Move constexpr_first/last to common.h This keeps it with constexpr_sum and the other metafunctions. --- include/pybind11/cast.h | 19 ------------------- include/pybind11/common.h | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 5f8eaf87a..c6abc0e8b 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1314,25 +1314,6 @@ private: std::tuple...> value; }; -NAMESPACE_BEGIN(constexpr_impl) -/// Implementation details for constexpr functions -constexpr int first(int i) { return i; } -template -constexpr int first(int i, T v, Ts... vs) { return v ? i : first(i + 1, vs...); } - -constexpr int last(int /*i*/, int result) { return result; } -template -constexpr int last(int i, int result, T v, Ts... vs) { return last(i + 1, v ? i : result, vs...); } -NAMESPACE_END(constexpr_impl) - -/// Return the index of the first type in Ts which satisfies Predicate -template class Predicate, typename... Ts> -constexpr int constexpr_first() { return constexpr_impl::first(0, Predicate::value...); } - -/// Return the index of the last type in Ts which satisfies Predicate -template class Predicate, typename... Ts> -constexpr int constexpr_last() { return constexpr_impl::last(0, -1, Predicate::value...); } - /// Helper class which collects only positional arguments for a Python function call. /// A fancier version below can collect any argument, but this one is optimal for simple calls. template diff --git a/include/pybind11/common.h b/include/pybind11/common.h index 4ebf89bbc..bbe92dfde 100644 --- a/include/pybind11/common.h +++ b/include/pybind11/common.h @@ -449,6 +449,26 @@ constexpr size_t constexpr_sum() { return 0; } template constexpr size_t constexpr_sum(T n, Ts... ns) { return size_t{n} + constexpr_sum(ns...); } +NAMESPACE_BEGIN(constexpr_impl) +/// Implementation details for constexpr functions +constexpr int first(int i) { return i; } +template +constexpr int first(int i, T v, Ts... vs) { return v ? i : first(i + 1, vs...); } + +constexpr int last(int /*i*/, int result) { return result; } +template +constexpr int last(int i, int result, T v, Ts... vs) { return last(i + 1, v ? i : result, vs...); } +NAMESPACE_END(constexpr_impl) + +/// Return the index of the first type in Ts which satisfies Predicate. Returns sizeof...(Ts) if +/// none match. +template class Predicate, typename... Ts> +constexpr int constexpr_first() { return constexpr_impl::first(0, Predicate::value...); } + +/// Return the index of the last type in Ts which satisfies Predicate, or -1 if none match. +template class Predicate, typename... Ts> +constexpr int constexpr_last() { return constexpr_impl::last(0, -1, Predicate::value...); } + // 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 {