[libc++] Define ostream nullptr inserter for >= C++17 only
authorJoe Loser <joeloser93@gmail.com>
Fri, 3 Jun 2022 23:05:23 +0000 (17:05 -0600)
committerJoe Loser <joeloser93@gmail.com>
Wed, 20 Jul 2022 00:16:45 +0000 (18:16 -0600)
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

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

index 283774585b925044aed097f340e4d274661043e8..ee220f3023aa5ffe1dc0c6ebc36fff37a030066d 100644 (file)
@@ -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);
index af411157e9cb4ce2379a8644ae85264f70328f61..e19d040ed1c265a77eace57b960c12cce87009ef 100644 (file)
@@ -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;
 }