Skip to content

Avoid string copy if possible when passing a Python object to std::ostream #3042

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
Jul 1, 2021

Conversation

jonathan-conder-sm
Copy link
Contributor

@jonathan-conder-sm jonathan-conder-sm commented Jun 17, 2021

Description

Changes operator<<(std::ostream &, const handle &) to delegate to operator<<(std::ostream &, std::string_view) if possible. If I understand the type caster for string_view correctly, I think this avoids an unnecessary copy.

The PYBIND11_HAS_STRING_VIEW macro is defined in cast.h, which is indirectly included by stl.h. If that's likely to change in future, it might be desirable to move the macro to internals.h or something.

Suggested changelog entry:

Use ``std::string_view`` if available to avoid a copy when passing an object to a ``std::ostream``.

@rwgk
Copy link
Collaborator

rwgk commented Jul 1, 2021

Thanks!

@rwgk rwgk merged commit 733f8de into pybind:master Jul 1, 2021
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Jul 1, 2021
@henryiii henryiii removed the needs changelog Possibly needs a changelog entry label Jul 13, 2021
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.

3 participants