std::basic_ostream operator<< overload for FixedString
authorAndrew Krieger <andrew.krieger@oculus.com>
Fri, 28 Jul 2017 15:20:05 +0000 (08:20 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Fri, 28 Jul 2017 15:27:19 +0000 (08:27 -0700)
Summary:
When attempting to output a FixedString into eg. glog or
some other ostream, it is first being implicitly converted to StringPiece
and then that is printed using the overloaded operator<<. If another
suitable implicit conversion is provided, eg. to `dynamic`, compilers
cannot choose between either one. Instead, overload operator<< directly
on FixedString to resolve the ambiguity.

Reviewed By: yfeldblum, ericniebler

Differential Revision: D5492779

fbshipit-source-id: 92d661e5471a91057d7a0d010420709c5d59232f

folly/FixedString.h

index 935cd8214b2489188ded057cf0b9146ed6e94730..bf0d52e32a0b10a44841113ebc3f1eb77582ea63 100644 (file)
@@ -2938,6 +2938,15 @@ class BasicFixedString : private detail::fixedstring::FixedStringBase {
   }
 };
 
+template <class C, std::size_t N>
+inline std::basic_ostream<C>& operator<<(
+    std::basic_ostream<C>& os,
+    const BasicFixedString<C, N>& string) {
+  using StreamSize = decltype(os.width());
+  os.write(string.begin(), static_cast<StreamSize>(string.size()));
+  return os;
+}
+
 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
  * Symmetric relational operators
  */