From 4a71f9cfcc0369d8b90412ab09ef7ae812440ef1 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Wed, 19 Sep 2018 18:29:57 +0000 Subject: [PATCH] Implement LWG 2221 - No formatted output operator for nullptr. Reviewed as https://reviews.llvm.org/D44263 llvm-svn: 342566 --- libcxx/include/ostream | 11 ++- .../ostream.inserters/nullptr.pass.cpp | 80 +++++++++++++++++++ libcxx/www/cxx1z_status.html | 4 +- 3 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 libcxx/test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters/nullptr.pass.cpp diff --git a/libcxx/include/ostream b/libcxx/include/ostream index 5404e0dca6c8..c29157077b48 100644 --- a/libcxx/include/ostream +++ b/libcxx/include/ostream @@ -56,6 +56,7 @@ public: basic_ostream& operator<<(double f); basic_ostream& operator<<(long double f); basic_ostream& operator<<(const void* p); + basic_ostream& operator<<(nullptr_t); // C++17 basic_ostream& operator<<(basic_streambuf* sb); // 27.7.2.7 Unformatted output: @@ -216,6 +217,7 @@ public: 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* __sb); // 27.7.2.7 Unformatted output: @@ -709,6 +711,14 @@ basic_ostream<_CharT, _Traits>::operator<<(const void* __n) return *this; } +template +basic_ostream<_CharT, _Traits>& +basic_ostream<_CharT, _Traits>::operator<<(nullptr_t) +{ + return *this << "(nullptr)"; +} + + template basic_ostream<_CharT, _Traits>& __put_character_sequence(basic_ostream<_CharT, _Traits>& __os, @@ -742,7 +752,6 @@ __put_character_sequence(basic_ostream<_CharT, _Traits>& __os, return __os; } - template basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c) diff --git a/libcxx/test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters/nullptr.pass.cpp b/libcxx/test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters/nullptr.pass.cpp new file mode 100644 index 000000000000..0893b2b07a84 --- /dev/null +++ b/libcxx/test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters/nullptr.pass.cpp @@ -0,0 +1,80 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template > +// class basic_ostream; + +// template +// basic_ostream& operator<<(basic_ostream& out, nullptr_t); +// + +#include +#include +#include "test_macros.h" + +template +class testbuf + : public std::basic_streambuf +{ + typedef std::basic_streambuf base; + std::basic_string str_; +public: + testbuf() + { + } + + std::basic_string str() const + {return std::basic_string(base::pbase(), base::pptr());} + +protected: + + virtual typename base::int_type + overflow(typename base::int_type ch = base::traits_type::eof()) + { + if (ch != base::traits_type::eof()) + { + int n = static_cast(str_.size()); + str_.push_back(ch); + str_.resize(str_.capacity()); + base::setp(const_cast(str_.data()), + const_cast(str_.data() + str_.size())); + base::pbump(n+1); + } + return ch; + } +}; + +int main() +{ + { + std::wostream os((std::wstreambuf*)0); + os << nullptr; + assert(os.bad()); + assert(os.fail()); + } + { + testbuf sb; + std::ostream os(&sb); + assert(sb.str().length() == 0); + os << nullptr; + assert(sb.str().length() > 0); + LIBCPP_ASSERT(sb.str() == "(nullptr)"); // output is an implementation-defined NTCTS + } + + { + testbuf sb; + std::wostream os(&sb); + assert(sb.str().length() == 0); + os << nullptr; + assert(sb.str().length() > 0); + LIBCPP_ASSERT(sb.str() == L"(nullptr)"); // output is an implementation-defined NTCTS + } +} diff --git a/libcxx/www/cxx1z_status.html b/libcxx/www/cxx1z_status.html index 6f3da4792908..c54a12f99e79 100644 --- a/libcxx/www/cxx1z_status.html +++ b/libcxx/www/cxx1z_status.html @@ -365,7 +365,7 @@ 2062Effect contradictions w/o no-throw guarantee of std::function swapsIssaquahComplete 2166Heap property underspecified?Issaquah - 2221No formatted output operator for nullptrIssaquah + 2221No formatted output operator for nullptrIssaquahComplete 2223shrink_to_fit effect on iterator validityIssaquahComplete 2261Are containers required to use their 'pointer' type internally?Issaquah 2394locale::name specification unclear - what is implementation-defined?IssaquahComplete @@ -504,7 +504,7 @@ -

Last Updated: 3-Aug-2018

+

Last Updated: 19-Sep-2018