Skip to content

Commit 94db2eb

Browse files
committed
Avoid LNK2001 in c++20 when /GL (Whole program optimization) is on with MSVC Update pybind11.h
1 parent d4f9cfb commit 94db2eb

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

include/pybind11/pybind11.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ PYBIND11_WARNING_DISABLE_CLANG("-Wgnu-zero-variadic-macro-arguments")
4343
# include <cxxabi.h>
4444
#endif
4545

46+
#if defined(__cpp_if_constexpr) && __cpp_if_constexpr >= 201606
47+
# define PYBIND11_MAYBE_CONSTEXPR constexpr
48+
#else
49+
# define PYBIND11_MAYBE_CONSTEXPR
50+
#endif
51+
4652
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
4753

4854
/* https://stackoverflow.com/questions/46798456/handling-gccs-noexcept-type-warning
@@ -3606,13 +3612,15 @@ function get_override(const T *this_ptr, const char *name) {
36063612
auto o = override(__VA_ARGS__); \
36073613
PYBIND11_WARNING_PUSH \
36083614
PYBIND11_WARNING_DISABLE_MSVC(4127) \
3609-
if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value \
3615+
if PYBIND11_MAYBE_CONSTEXPR ( \
3616+
pybind11::detail::cast_is_temporary_value_reference<ret_type>::value \
36103617
&& !pybind11::detail::is_same_ignoring_cvref<ret_type, PyObject *>::value) { \
36113618
static pybind11::detail::override_caster_t<ret_type> caster; \
36123619
return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \
3620+
} else { \
3621+
return pybind11::detail::cast_safe<ret_type>(std::move(o)); \
36133622
} \
36143623
PYBIND11_WARNING_POP \
3615-
return pybind11::detail::cast_safe<ret_type>(std::move(o)); \
36163624
} \
36173625
} while (false)
36183626

0 commit comments

Comments
 (0)