From a18429c13fa8d0ad46cf65419f896afed41b316e Mon Sep 17 00:00:00 2001 From: Laramie Leavitt Date: Mon, 11 Apr 2022 10:47:01 -0700 Subject: [PATCH 1/5] Cleanup cast_safe specialization Replace explicit specialization of cast_safe with SFINAE. It's better for SFINAE cases to cover all type-sets rather than mixing SFINAE and explicit specialization. Extracted from #3674 --- include/pybind11/cast.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index bead97f914..e1ce8215ae 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1155,15 +1155,19 @@ enable_if_t::value, T> cast_ref(object &&, // static_assert, even though if it's in dead code, so we provide a "trampoline" to pybind11::cast // that only does anything in cases where pybind11::cast is valid. template -enable_if_t::value, T> cast_safe(object &&o) { +enable_if_t>::value && + !cast_is_temporary_value_reference::value, T> +cast_safe(object &&o) { return pybind11::cast(std::move(o)); } template -enable_if_t::value, T> cast_safe(object &&) { +enable_if_t::value, T> +cast_safe(object &&) { pybind11_fail("Internal error: cast_safe fallback invoked"); } -template <> -inline void cast_safe(object &&) {} +template +enable_if_t>::value, void> +cast_safe(object &&) {} PYBIND11_NAMESPACE_END(detail) From 4f75909ee312b2f90affce3195583e8a441e4a2d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 11 Apr 2022 17:48:02 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- include/pybind11/cast.h | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index e1ce8215ae..928b6a4eb5 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1155,19 +1155,18 @@ enable_if_t::value, T> cast_ref(object &&, // static_assert, even though if it's in dead code, so we provide a "trampoline" to pybind11::cast // that only does anything in cases where pybind11::cast is valid. template -enable_if_t>::value && - !cast_is_temporary_value_reference::value, T> +enable_if_t>::value + && !cast_is_temporary_value_reference::value, + T> cast_safe(object &&o) { return pybind11::cast(std::move(o)); } template -enable_if_t::value, T> -cast_safe(object &&) { +enable_if_t::value, T> cast_safe(object &&) { pybind11_fail("Internal error: cast_safe fallback invoked"); } template -enable_if_t>::value, void> -cast_safe(object &&) {} +enable_if_t>::value, void> cast_safe(object &&) {} PYBIND11_NAMESPACE_END(detail) From 6bbf1362da56a4a8c1081916307b7641a0d87500 Mon Sep 17 00:00:00 2001 From: Laramie Leavitt Date: Mon, 11 Apr 2022 10:55:12 -0700 Subject: [PATCH 3/5] Update cast.h Use detail::none_of<> as suggested --- include/pybind11/cast.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 928b6a4eb5..d6efc12e15 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1155,8 +1155,8 @@ enable_if_t::value, T> cast_ref(object &&, // static_assert, even though if it's in dead code, so we provide a "trampoline" to pybind11::cast // that only does anything in cases where pybind11::cast is valid. template -enable_if_t>::value - && !cast_is_temporary_value_reference::value, +enable_if_t>, + cast_is_temporary_value_reference>::value, T> cast_safe(object &&o) { return pybind11::cast(std::move(o)); From 3f7fd17bae7bcd623a3a79d31e5156ae26ae7a31 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 11 Apr 2022 17:55:46 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- include/pybind11/cast.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index d6efc12e15..df26311899 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1155,7 +1155,7 @@ enable_if_t::value, T> cast_ref(object &&, // static_assert, even though if it's in dead code, so we provide a "trampoline" to pybind11::cast // that only does anything in cases where pybind11::cast is valid. template -enable_if_t>, +enable_if_t>, cast_is_temporary_value_reference>::value, T> cast_safe(object &&o) { From b3ed3b8198351bf53af16d191b3290a3b57e80d5 Mon Sep 17 00:00:00 2001 From: Laramie Leavitt Date: Mon, 11 Apr 2022 11:25:15 -0700 Subject: [PATCH 5/5] Update cast.h Reorder: If TEMP_REF If VOID if (!VOID && !TEMP_REF) --- include/pybind11/cast.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index df26311899..e8128710e2 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1155,18 +1155,18 @@ enable_if_t::value, T> cast_ref(object &&, // static_assert, even though if it's in dead code, so we provide a "trampoline" to pybind11::cast // that only does anything in cases where pybind11::cast is valid. template -enable_if_t>, - cast_is_temporary_value_reference>::value, - T> -cast_safe(object &&o) { - return pybind11::cast(std::move(o)); -} -template enable_if_t::value, T> cast_safe(object &&) { pybind11_fail("Internal error: cast_safe fallback invoked"); } template enable_if_t>::value, void> cast_safe(object &&) {} +template +enable_if_t, + std::is_same>>::value, + T> +cast_safe(object &&o) { + return pybind11::cast(std::move(o)); +} PYBIND11_NAMESPACE_END(detail)