From e496e7eaf12894c1bb7a3b7debdf90c9d55c5937 Mon Sep 17 00:00:00 2001 From: Jonathan Conder Date: Thu, 17 Jun 2021 14:30:35 +1200 Subject: [PATCH] Avoid string copy if possible when passing a Python object to std::ostream --- include/pybind11/stl.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 18fbafb1e2..5844d3de7b 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -377,7 +377,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; }