Skip to content

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

Merged
rwgk merged 1 commit into
pybind:masterfrom
jonathan-conder-sm:ostream_string_view
Jul 1, 2021
Merged

Avoid string copy if possible when passing a Python object to std::ostream#3042
rwgk merged 1 commit into
pybind:masterfrom
jonathan-conder-sm:ostream_string_view

Conversation

@jonathan-conder-sm

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

Copy link
Copy Markdown
Contributor

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

rwgk commented Jul 1, 2021

Copy link
Copy Markdown
Collaborator

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