From 733f8de24feed964f96b639a0a44247f46bed868 Mon Sep 17 00:00:00 2001 From: jonathan-conder-sm <63538679+jonathan-conder-sm@users.noreply.github.com> Date: Thu, 1 Jul 2021 17:19:14 +1200 Subject: [PATCH] Avoid string copy if possible when passing a Python object to std::ostream (#3042) --- include/pybind11/stl.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 61d3fba61..ca20b7483 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -380,7 +380,11 @@ struct type_caster> : variant_caster> { PYBIND11_NAMESPACE_END(detail) inline std::ostream &operator<<(std::ostream &os, const handle &obj) { +#ifdef PYBIND11_HAS_STRING_VIEW + os << str(obj).cast(); +#else os << (std::string) str(obj); +#endif return os; }