Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ PYBIND11_WARNING_DISABLE_CLANG("-Wgnu-zero-variadic-macro-arguments")
# include <cxxabi.h>
#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
Expand Down Expand Up @@ -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<ret_type>::value \
if PYBIND11_MAYBE_CONSTEXPR ( \
pybind11::detail::cast_is_temporary_value_reference<ret_type>::value \
&& !pybind11::detail::is_same_ignoring_cvref<ret_type, PyObject *>::value) { \
static pybind11::detail::override_caster_t<ret_type> caster; \
return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \
} else { \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the else here? The other if branch ends with a return, the else seems redundant.

I think it'd be slightly better to leave the code down here as-is, in part because the PYBIND11_WARNING_POP is then before the return below.

Copy link
Contributor Author

@hellokenlee hellokenlee Dec 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The else is added here because the CI checks complained “warning C4702: unreachable code”( in which case the constexpr if evals true, and the code outside that constexpr if becomes unreachable ). Since the CI checks turn on "warning as error", the CI checks will failed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late reply.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation!

The warning will be annoying, seems like a second C4127, not keeping up with the times (templates), so I think we'll probably wind up suppressing C4702 as we make more use of constexpr in the future ... but your solution is good for now.

return pybind11::detail::cast_safe<ret_type>(std::move(o)); \
} \
PYBIND11_WARNING_POP \
return pybind11::detail::cast_safe<ret_type>(std::move(o)); \
} \
} while (false)

Expand Down
Loading