Skip to content

fix: returning a copyable type with a deleted move constructor - #6143

Merged
henryiii merged 2 commits into
pybind:masterfrom
henryiii:fix/function-ref-copy-only-return
Aug 14, 2026
Merged

fix: returning a copyable type with a deleted move constructor#6143
henryiii merged 2 commits into
pybind:masterfrom
henryiii:fix/function-ref-copy-only-return

Conversation

@henryiii

Copy link
Copy Markdown
Collaborator

🤖 AI text below 🤖

Description

Fixes #6142, a 3.1.0 regression from the call_impl outlining in #5887.

detail::function_ref's constructor SFINAE requires is_convertible<decltype(f(args...)), Ret>. For a copyable type whose move constructor is explicitly deleted, is_convertible<Ret, Ret> is false, because the trait tests conversion from an xvalue, which selects the deleted move constructor. In C++17 such a prvalue return is legal via guaranteed copy elision, so the constraint rejected code the function body compiles fine. The fix also accepts an exact type match (C++17 and later only; in C++14 such a type cannot be returned by value at all).

The regression test fails to compile without the fix and is guarded for C++17, since the C++14 CI builds cannot express the case.

Suggested changelog entry:

  • Fixed a 3.1.0 regression: functions returning a copyable type with an explicitly deleted move constructor failed to compile.

…d#6142)

detail::function_ref's constructor SFINAE required
is_convertible<Ret, Ret>, which is false for a copyable type whose move
constructor is explicitly deleted (the trait tests conversion from an
xvalue). In C++17, such a prvalue return is legal via guaranteed copy
elision, so also accept an exact type match. Regression introduced in
3.1.0 by the call_impl outlining (pybind#5887).

Fixes pybind#6142

Assisted-by: ClaudeCode:claude-fable-5
Comment thread include/pybind11/detail/function_ref.h Outdated
Comment thread tests/test_copy_move.cpp Outdated
Comment thread tests/test_copy_move.cpp Outdated
Per review: the feature-test macro expresses the intent more precisely
than PYBIND11_CPP17.

Assisted-by: ClaudeCode:claude-opus-5
@henryiii
henryiii merged commit 0599909 into pybind:master Aug 14, 2026
78 checks passed
@henryiii
henryiii deleted the fix/function-ref-copy-only-return branch August 14, 2026 20:33
@github-actions github-actions Bot added the needs changelog Possibly needs a changelog entry label Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs changelog Possibly needs a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Possible 3.1.0 regression when returning an object that has a copy constructor but no move constructor

2 participants