diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 61d3fba6..ca20b748 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; }