fix: bind noexcept and ref-qualified methods from unregistered base classes#5992
Open
Skylion007 wants to merge 11 commits intopybind:masterfrom
Open
fix: bind noexcept and ref-qualified methods from unregistered base classes#5992Skylion007 wants to merge 11 commits intopybind:masterfrom
Skylion007 wants to merge 11 commits intopybind:masterfrom
Conversation
a7a3ed9 to
e8b0584
Compare
e8b0584 to
b303194
Compare
ad30bc0 to
f530adb
Compare
Skylion007
commented
Feb 20, 2026
Skylion007
commented
Feb 20, 2026
Skylion007
commented
Feb 22, 2026
| // (which match plain Return(Args...)) work correctly with noexcept callables (issue #2234). | ||
| template <typename Function, typename F = remove_reference_t<Function>> | ||
| using function_signature_t = conditional_t< | ||
| using function_signature_t = remove_noexcept_t<conditional_t< |
Collaborator
Author
There was a problem hiding this comment.
Should I normalize it here for all pybind11? Or keep it as utility that and create a new alias that always strips the noexcept
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fix issue #2234 . Fix overloading to accept noexcept function types too and strip them. Mostly authored by ClaudeCode with a lot of human intervention.
fix: support noexcept and ref-qualified methods from unregistered base classes (issue #2234)
Problem
In C++17,
noexceptbecame part of the function type (P0012R1). This meansint (Base::*)() const noexceptandint (Base::*)() constare distincttypes. When a derived class inherits methods from an unregistered base,
method_adaptormust recast the member function pointer fromBase::*toDerived::*. Without explicit template specializations for noexcept (andref-qualified) variants, the generic
F&&fallback was selected, leaving thepointer typed as
Base::*— causing pybind11 to look upBaseasself,which fails at runtime for unregistered base classes.
Similarly,
cpp_functionlacked constructors fornoexcept,& noexcept,const noexcept,const & noexcept,&&,const &&,&& noexcept, andconst && noexceptmember function pointer types, so those overloads silentlyfell through to the generic lambda constructor.
Solution
include/pybind11/pybind11.hcpp_functionconstructors for all missing qualifier combinations:&&,const &&(unconditional): usestd::move(*c).*fin the lambdanoexcept,const noexcept,& noexcept,const & noexcept,&& noexcept,const && noexcept(under#ifdef __cpp_noexcept_function_type)detail::rebind_member_ptr<Derived, T>type trait mappingReturn (Base::*)(Args...) qualifierstoReturn (Derived::*)(Args...) qualifiersacross all 12 qualifier combinations, generated via a local
PYBIND11_REBIND_MEMBER_PTRmacro.detail::adapt_member_ptr<Derived>(pmf)— sharedconstexprhelpercalled by all
method_adaptoroverloads: runs theis_accessible_base_ofstatic_assert then returns the implicitly-cast pointer.
method_adaptoroverloads with aPYBIND11_METHOD_ADAPTORmacro covering all 11 qualified variants (plus the no-qualifier overload
written explicitly to avoid MSVC C4003). All overloads are
constexpr.&/const &method_adaptoroverloads (no#ifdefguard) to fix C++14 builds where
noexceptis stripped from the type butthe
&qualifier is not.include/pybind11/numpy.hvectorizeoverloads fornoexceptfree function pointers.Tests
test_methods_and_attributes:NoexceptDerived/NoexceptUnregisteredBase(C++17 noexcept from unregistered base),
RValueRefDerived/RValueRefUnregisteredBase(&&/const &&methods that move-out state toconfirm
std::move(*c).*fsemantics),NoexceptOverloaded(overload_castwith noexcept), static_asserts verifying
method_adaptorpreserves allqualifier combinations.
test_buffers:def_bufferwithnoexceptmember function pointers.test_numpy_vectorize:vectorizewithnoexceptfree function pointers.Compatibility
cpp_functionconstructorsand
method_adaptoroverloads are additive and selected only for the newtype signatures.
__cpp_noexcept_function_type).rather than invoking macros with an empty argument.
bugprone-macro-parenthesessuppressed withNOLINTBEGIN/NOLINTENDaround the macro definitions (thequalifiersargument appearsin type position where parenthesizing it would be invalid C++).
Suggested changelog entry:
noexceptand ref-qualified (&,&&) methodsinherited from unregistered base classes in C++17, where
noexceptispart of the function type.
method_adaptornow has specializations forall qualifier combinations (
const,&,const &,&&,const &&, and theirnoexceptvariants), andcpp_functiongainsmatching constructors.
&&-qualified methods correctly usestd::move(*self).*fin the generated lambda.