From 94db2ebff889ea71d983456c9165db0a70b41c3e Mon Sep 17 00:00:00 2001 From: KenLee Date: Wed, 17 Dec 2025 19:22:57 +0800 Subject: [PATCH] Avoid LNK2001 in c++20 when /GL (Whole program optimization) is on with MSVC Update pybind11.h --- include/pybind11/pybind11.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 91b38d91ed..f93272628d 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -43,6 +43,12 @@ PYBIND11_WARNING_DISABLE_CLANG("-Wgnu-zero-variadic-macro-arguments") # include #endif +#if defined(__cpp_if_constexpr) && __cpp_if_constexpr >= 201606 +# define PYBIND11_MAYBE_CONSTEXPR constexpr +#else +# define PYBIND11_MAYBE_CONSTEXPR +#endif + PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) /* https://stackoverflow.com/questions/46798456/handling-gccs-noexcept-type-warning @@ -3606,13 +3612,15 @@ function get_override(const T *this_ptr, const char *name) { auto o = override(__VA_ARGS__); \ PYBIND11_WARNING_PUSH \ PYBIND11_WARNING_DISABLE_MSVC(4127) \ - if (pybind11::detail::cast_is_temporary_value_reference::value \ + if PYBIND11_MAYBE_CONSTEXPR ( \ + pybind11::detail::cast_is_temporary_value_reference::value \ && !pybind11::detail::is_same_ignoring_cvref::value) { \ static pybind11::detail::override_caster_t caster; \ return pybind11::detail::cast_ref(std::move(o), caster); \ + } else { \ + return pybind11::detail::cast_safe(std::move(o)); \ } \ PYBIND11_WARNING_POP \ - return pybind11::detail::cast_safe(std::move(o)); \ } \ } while (false)