Skip to content

Use rvalue reference for std::variant cast_op<T> #3811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2022
Merged

Use rvalue reference for std::variant cast_op<T> #3811

merged 1 commit into from
Mar 18, 2022

Conversation

laramiel
Copy link
Contributor

@laramiel laramiel commented Mar 18, 2022

Nearly every call site of cast_op<T> uses an r-value reference.

Except in stl.h variant_caster::load_alternative which handles std::variant.

Fix that.

Nearly every call site of cast_op<T> uses an r-value reference.

Except stl.h variant_caster::load_alternative for handling std::variant.

Fix that.
@laramiel
Copy link
Contributor Author

NOTE: I have not yet run extensive tests on this.

Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

The CI is happy, what could possibly go wrong! :-)
I'll try to run our global testing tonight with this included.

@laramiel
Copy link
Contributor Author

laramiel commented Mar 18, 2022

It should be safe, the caster is essentially discarded on the next line. It may change a few call sites from a copy to a move, which is more consistent with basically all other cast operations. So it might improve casting of things like std::variant<std::unique_ptr<X>>.

@laramiel
Copy link
Contributor Author

laramiel commented Mar 18, 2022

Also, it's possible that this overload of cast_op isn't a good idea: (cast.h#58)

typename make_caster<T>::template cast_op_type<T> cast_op(make_caster<T> &caster) {
    return caster.operator typename make_caster<T>::template cast_op_type<T>();
}

Because it will change the default T_ for the template type_caster<T>::cast_op_type<T_>. And it will likely pessimize type_casters to use copies more than necessary.

Changing it will break code. I think that the only current use of that overload is in the std::reference_wrapper type_caster. But who knows about other users.

Not a high priority.

@rwgk
Copy link
Collaborator

rwgk commented Mar 18, 2022

Google global testing with this PR passed (2022-03-18 05:00 batch, test token OCL:435561515:BASE:435643811:1647617193745:53ea324).

@rwgk
Copy link
Collaborator

rwgk commented Mar 18, 2022

I'll go ahead merging this 1-line change, based on the extensive testing, to get it into the smart_holder update I'm working on right now.

@rwgk rwgk merged commit b3a43d1 into pybind:master Mar 18, 2022
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Mar 18, 2022
@rwgk rwgk removed the needs changelog Possibly needs a changelog entry label Mar 18, 2022
@laramiel laramiel deleted the patch-1 branch March 22, 2022 08:21
henryiii pushed a commit to henryiii/pybind11 that referenced this pull request Mar 25, 2022
Nearly every call site of cast_op<T> uses an r-value reference.

Except stl.h variant_caster::load_alternative for handling std::variant.

Fix that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants