From 510df14124259ef19b27fd90554905ce6dbcd4d5 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Wed, 14 Jul 2021 14:14:07 -0400 Subject: [PATCH 1/2] Enable constexpr in casting for C++17 --- include/pybind11/cast.h | 25 ++++++++++++++++++------- include/pybind11/detail/common.h | 6 ++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index a748c77c0f..92cedec71a 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -887,13 +887,24 @@ T cast(const handle &handle) { return T(reinterpret_borrow(handle)); } template ::value, int> = 0> object cast(T &&value, return_value_policy policy = return_value_policy::automatic_reference, handle parent = handle()) { - using no_ref_T = typename std::remove_reference::type; - if (policy == return_value_policy::automatic) - policy = std::is_pointer::value ? return_value_policy::take_ownership : - std::is_lvalue_reference::value ? return_value_policy::copy : return_value_policy::move; - else if (policy == return_value_policy::automatic_reference) - policy = std::is_pointer::value ? return_value_policy::reference : - std::is_lvalue_reference::value ? return_value_policy::copy : return_value_policy::move; + // using no_ref_T = PYBIND11_CPP17_CONSTEXPR std::remove_reference::type; + if (policy == return_value_policy::automatic) { + if PYBIND11_CPP17_CONSTEXPR (std::is_pointer>::value) { + policy = return_value_policy::take_ownership; + } else if PYBIND11_CPP17_CONSTEXPR (std::is_lvalue_reference::value) { + policy = return_value_policy::copy; + } else { + policy = return_value_policy::move; + } + } else if (policy == return_value_policy::automatic_reference) { + if PYBIND11_CPP17_CONSTEXPR (std::is_pointer>::value) { + policy = return_value_policy::reference; + } else if PYBIND11_CPP17_CONSTEXPR (std::is_lvalue_reference::value) { + policy = return_value_policy::copy; + } else { + policy = return_value_policy::move; + } + } return reinterpret_steal(detail::make_caster::cast(std::forward(value), policy, parent)); } diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index f804e7c85a..3c16a329e6 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -102,6 +102,12 @@ # define PYBIND11_MAYBE_UNUSED __attribute__ ((__unused__)) #endif +#if defined(PYBIND11_CPP17) +# define PYBIND11_CPP17_CONSTEXPR constexpr +#else +# define PYBIND11_CPP17_CONSTEXPR +#endif + /* Don't let Python.h #define (v)snprintf as macro because they are implemented properly in Visual Studio since 2015. */ #if defined(_MSC_VER) && _MSC_VER >= 1900 From 7948b6b53dd9cd512b9bdbcde5d88b0efb222cb2 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Wed, 14 Jul 2021 14:15:59 -0400 Subject: [PATCH 2/2] Remove comment --- include/pybind11/cast.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 92cedec71a..b478ed3491 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -887,7 +887,6 @@ T cast(const handle &handle) { return T(reinterpret_borrow(handle)); } template ::value, int> = 0> object cast(T &&value, return_value_policy policy = return_value_policy::automatic_reference, handle parent = handle()) { - // using no_ref_T = PYBIND11_CPP17_CONSTEXPR std::remove_reference::type; if (policy == return_value_policy::automatic) { if PYBIND11_CPP17_CONSTEXPR (std::is_pointer>::value) { policy = return_value_policy::take_ownership;