Skip to content

Commit 733f8de

Browse files
Avoid string copy if possible when passing a Python object to std::ostream (#3042)
1 parent cad79c1 commit 733f8de

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

include/pybind11/stl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,11 @@ struct type_caster<std::variant<Ts...>> : variant_caster<std::variant<Ts...>> {
380380
PYBIND11_NAMESPACE_END(detail)
381381

382382
inline std::ostream &operator<<(std::ostream &os, const handle &obj) {
383+
#ifdef PYBIND11_HAS_STRING_VIEW
384+
os << str(obj).cast<std::string_view>();
385+
#else
383386
os << (std::string) str(obj);
387+
#endif
384388
return os;
385389
}
386390

0 commit comments

Comments
 (0)