[libcxx-commits] [PATCH] D63053: Add nullptr output operator overload (2221)

Zoe Carver via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jun 8 16:35:52 PDT 2019


zoecarver created this revision.
zoecarver added reviewers: mclow.lists, EricWF, ldionne.
Herald added subscribers: libcxx-commits, dexonsmith.

This patch fixes LWG issue 2221 <https://cplusplus.github.io/LWG/issue2221> by adding a `nullptr` overload to `basic_ostream`.


Repository:
  rCXX libc++

https://reviews.llvm.org/D63053

Files:
  include/ostream
  test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters/streambuf.pass.cpp


Index: test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters/streambuf.pass.cpp
===================================================================
--- test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters/streambuf.pass.cpp
+++ test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters/streambuf.pass.cpp
@@ -67,6 +67,12 @@
         os << &sb2;
         assert(sb.str() == "testing...");
     }
+    { // LWG 2221 - nullptr
+        testbuf<char> sb;
+        std::ostream os(&sb);
+        os << nullptr;
+        assert(sb.str().c_str()[0] == '0');
+    }
 
   return 0;
 }
Index: include/ostream
===================================================================
--- include/ostream
+++ include/ostream
@@ -216,6 +216,7 @@
     basic_ostream& operator<<(double __f);
     basic_ostream& operator<<(long double __f);
     basic_ostream& operator<<(const void* __p);
+    basic_ostream& operator<<(nullptr_t);
     basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb);
 
     // 27.7.2.7 Unformatted output:
@@ -709,6 +710,13 @@
     return *this;
 }
 
+template<class _CharT, class _Traits>
+basic_ostream<_CharT, _Traits>&
+basic_ostream<_CharT, _Traits>::operator<<(nullptr_t)
+{
+    return *this << static_cast<const void*>(0x0);
+}
+
 template<class _CharT, class _Traits>
 basic_ostream<_CharT, _Traits>&
 __put_character_sequence(basic_ostream<_CharT, _Traits>& __os,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63053.203715.patch
Type: text/x-patch
Size: 1500 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20190608/7f93adbb/attachment.bin>


More information about the libcxx-commits mailing list