Avoid string copy if possible when passing a Python object to std::ostream (#3042)

This commit is contained in:
jonathan-conder-sm 2021-07-01 17:19:14 +12:00 committed by GitHub
parent cad79c1146
commit 733f8de24f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 0 deletions

View File

@ -380,7 +380,11 @@ struct type_caster<std::variant<Ts...>> : variant_caster<std::variant<Ts...>> {
PYBIND11_NAMESPACE_END(detail)
inline std::ostream &operator<<(std::ostream &os, const handle &obj) {
#ifdef PYBIND11_HAS_STRING_VIEW
os << str(obj).cast<std::string_view>();
#else
os << (std::string) str(obj);
#endif
return os;
}