Fix StringPiece ostream overloads to properly not rely on <ostream>
authorAndrew Krieger <andrew.krieger@oculus.com>
Fri, 28 Jul 2017 15:20:06 +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:
std::ostream may be incomplete because Range.h doesn't use <ostream>,
only <iosfwd>. Changing the operator<< overloads to be templated on the char
type defers typechecking until callsites, which will avoid the potential problem.

Reviewed By: yfeldblum, ericniebler

Differential Revision: D5494648

fbshipit-source-id: e59b6fdfba6c08ec70ebb1e10c14a43307a1119f

folly/Range.h

index efe34908d17dfb1430eaa0fc31f57c374af8b538..a2c1d96cc91153e87f1843fb8a4c6b38a4e5cb5a 100644 (file)
@@ -970,15 +970,19 @@ typedef Range<char*> MutableStringPiece;
 typedef Range<const unsigned char*> ByteRange;
 typedef Range<unsigned char*> MutableByteRange;
 
-inline std::ostream& operator<<(std::ostream& os,
-                                const StringPiece piece) {
-  os.write(piece.start(), std::streamsize(piece.size()));
+template <class C>
+std::basic_ostream<C>& operator<<(
+    std::basic_ostream<C>& os,
+    Range<C const*> piece) {
+  using StreamSize = decltype(os.width());
+  os.write(piece.start(), static_cast<StreamSize>(piece.size()));
   return os;
 }
 
-inline std::ostream& operator<<(std::ostream& os,
-                                const MutableStringPiece piece) {
-  os.write(piece.start(), std::streamsize(piece.size()));
+template <class C>
+std::basic_ostream<C>& operator<<(std::basic_ostream<C>& os, Range<C*> piece) {
+  using StreamSize = decltype(os.width());
+  os.write(piece.start(), static_cast<StreamSize>(piece.size()));
   return os;
 }