fix: returning a copyable type with a deleted move constructor - #6143
Open
henryiii wants to merge 1 commit into
Open
fix: returning a copyable type with a deleted move constructor#6143henryiii wants to merge 1 commit into
henryiii wants to merge 1 commit into
Conversation
…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
Skylion007
reviewed
Aug 12, 2026
| // copy elision. Accept that case explicitly. See issue #6142. | ||
| template <typename From, typename To> | ||
| using is_returnable_as = bool_constant< | ||
| #if defined(PYBIND11_CPP17) |
Collaborator
There was a problem hiding this comment.
Guard with feature flag: "__cpp_guaranteed_copy_elision" :) seems more specific here and better expresses intent.
#include <iostream>
int main() {
#if defined(__cpp_guaranteed_copy_elision) && __cpp_guaranteed_copy_elision >= 201606L
std::cout << "Guaranteed prvalue elision is supported!\n";
#else
std::cout << "Old value categories apply (copies/moves may occur).\n";
#endif
}| // #6142: returning a copy-constructible type with an explicitly deleted move | ||
| // constructor failed to compile inside detail::function_ref (3.1.0 regression). | ||
| // Returning such a type by value requires C++17 guaranteed copy elision. | ||
| #if defined(PYBIND11_CPP17) |
| .def_static("get_one", &lacking_move_ctor::get_one, py::return_value_policy::move); | ||
|
|
||
| // test_copy_only_deleted_move (#6142) | ||
| #if defined(PYBIND11_CPP17) |
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.
🤖 AI text below 🤖
Description
Fixes #6142, a 3.1.0 regression from the
call_imploutlining in #5887.detail::function_ref's constructor SFINAE requiresis_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: