From 088ad4f2987d3da4b1ffab376b2b4812fd7634ef Mon Sep 17 00:00:00 2001 From: Laramie Leavitt Date: Mon, 11 Apr 2022 11:57:05 -0700 Subject: [PATCH] Cleanup cast_safe specialization (#3861) * 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 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update cast.h Use detail::none_of<> as suggested * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update cast.h Reorder: If TEMP_REF If VOID if (!VOID && !TEMP_REF) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- include/pybind11/cast.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index bead97f91..e8128710e 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1155,15 +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, 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 <> -inline void cast_safe(object &&) {} +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)