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; }