From 50cfb76e029bbfda21622d4f00c9fe8a2a7099a6 Mon Sep 17 00:00:00 2001 From: Joe Loser Date: Fri, 3 Jun 2022 17:05:23 -0600 Subject: [PATCH] [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 --- libcxx/include/ostream | 4 ++++ .../ostream.formatted/ostream.inserters/streambuf.pass.cpp | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/libcxx/include/ostream b/libcxx/include/ostream index 2837745..ee220f3 100644 --- a/libcxx/include/ostream +++ b/libcxx/include/ostream @@ -225,9 +225,13 @@ public: basic_ostream& operator<<(basic_streambuf* __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 af41115..e19d040 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 sb; std::ostream os(&sb); os << nullptr; assert(sb.str().size() != 0); LIBCPP_ASSERT(sb.str() == "nullptr"); } +#endif return 0; } -- 2.7.4