[libcxx-commits] [libcxx] 50cfb76 - [libc++] Define ostream nullptr inserter for >= C++17 only

Joe Loser via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jul 19 17:18:15 PDT 2022


Author: Joe Loser
Date: 2022-07-19T18:16:45-06:00
New Revision: 50cfb76e029bbfda21622d4f00c9fe8a2a7099a6

URL: https://github.com/llvm/llvm-project/commit/50cfb76e029bbfda21622d4f00c9fe8a2a7099a6
DIFF: https://github.com/llvm/llvm-project/commit/50cfb76e029bbfda21622d4f00c9fe8a2a7099a6.diff

LOG: [libc++] Define ostream nullptr inserter for >= C++17 only

The `ostream` `nullptr` inserter implemented in 3c125fe is missing a C++ version
guard. Normally, `libc++` takes the stance of backporting LWG issues to older
standards modes as was done in 3c125fe. However, backporting to older standards
modes breaks existing code in popular libraries such as `Boost.Test` and
`Google Test` who define their own overload for `nullptr_t`.

Instead, only apply this `operator<<` overload in C++17 or later.

Fixes https://github.com/llvm/llvm-project/issues/55861.

Differential Revision: https://reviews.llvm.org/D127033

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/libcxx/include/ostream b/libcxx/include/ostream
index 283774585b925..ee220f3023aa5 100644
--- a/libcxx/include/ostream
+++ b/libcxx/include/ostream
@@ -225,9 +225,13 @@ public:
 
     basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb);
 
+#if _LIBCPP_STD_VER > 14
+// LWG 2221 - nullptr. This is not backported to older standards modes.
+// See https://reviews.llvm.org/D127033 for more info on the rationale.
     _LIBCPP_INLINE_VISIBILITY
     basic_ostream& operator<<(nullptr_t)
     { return *this << "nullptr"; }
+#endif
 
     // 27.7.2.7 Unformatted output:
     basic_ostream& put(char_type __c);

diff  --git a/libcxx/test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters/streambuf.pass.cpp b/libcxx/test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters/streambuf.pass.cpp
index af411157e9cb4..e19d040ed1c26 100644
--- a/libcxx/test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters/streambuf.pass.cpp
+++ b/libcxx/test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters/streambuf.pass.cpp
@@ -67,13 +67,17 @@ int main(int, char**)
         os << &sb2;
         assert(sb.str() == "testing...");
     }
-    { // LWG 2221 - nullptr
+#if TEST_STD_VER > 14
+// LWG 2221 - nullptr. This is not backported to older standards modes.
+// See https://reviews.llvm.org/D127033 for more info on the rationale.
+    {
         testbuf<char> sb;
         std::ostream os(&sb);
         os << nullptr;
         assert(sb.str().size() != 0);
         LIBCPP_ASSERT(sb.str() == "nullptr");
     }
+#endif
 
   return 0;
 }


        


More information about the libcxx-commits mailing list