diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index a748c77c0f..b478ed3491 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -887,13 +887,23 @@ 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; + 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