Index: include/ostream =================================================================== --- include/ostream +++ include/ostream @@ -56,6 +56,7 @@ 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 @@ 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 @@ 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 @@ return __os; } - template basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c) Index: test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters/nullptr.pass.cpp =================================================================== --- test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters/nullptr.pass.cpp +++ 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 + } +}