diff --git a/libcxx/include/istream b/libcxx/include/istream index bfbe5f24728e..5145157aa42a 100644 --- a/libcxx/include/istream +++ b/libcxx/include/istream @@ -150,9 +150,9 @@ template basic_istream& ws(basic_istream& is); -template - basic_istream& - operator>>(basic_istream&& is, T& x); +// rvalue stream extraction +template + Stream&& operator>>(Stream&& is, T&& x); } // std @@ -1378,13 +1378,23 @@ ws(basic_istream<_CharT, _Traits>& __is) #ifndef _LIBCPP_CXX03_LANG -template -inline _LIBCPP_INLINE_VISIBILITY -basic_istream<_CharT, _Traits>& -operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp&& __x) +template +struct __is_istreamable : false_type { }; + +template +struct __is_istreamable<_Stream, _Tp, decltype( + _VSTD::declval<_Stream>() >> _VSTD::declval<_Tp>(), void() +)> : true_type { }; + +template , + __is_istreamable<_Stream&, _Tp&&>>::value +>> +_LIBCPP_INLINE_VISIBILITY +_Stream&& operator>>(_Stream&& __is, _Tp&& __x) { __is >> _VSTD::forward<_Tp>(__x); - return __is; + return _VSTD::move(__is); } #endif // _LIBCPP_CXX03_LANG diff --git a/libcxx/include/ostream b/libcxx/include/ostream index 697732d54e6d..fc0bf69e9d92 100644 --- a/libcxx/include/ostream +++ b/libcxx/include/ostream @@ -126,9 +126,8 @@ template basic_ostream& flush(basic_ostream& os); // rvalue stream insertion -template - basic_ostream& - operator<<(basic_ostream&& os, const T& x); +template + Stream&& operator<<(Stream&& os, const T& x); } // std @@ -1028,15 +1027,20 @@ flush(basic_ostream<_CharT, _Traits>& __os) #ifndef _LIBCPP_CXX03_LANG +template +struct __is_ostreamable : false_type { }; + template -inline _LIBCPP_INLINE_VISIBILITY -typename enable_if -< - !is_lvalue_reference<_Stream>::value && - is_base_of::value, - _Stream&& ->::type -operator<<(_Stream&& __os, const _Tp& __x) +struct __is_ostreamable<_Stream, _Tp, decltype( + _VSTD::declval<_Stream>() << _VSTD::declval<_Tp>(), void() +)> : true_type { }; + +template , + __is_ostreamable<_Stream&, const _Tp&>>::value +>> +_LIBCPP_INLINE_VISIBILITY +_Stream&& operator<<(_Stream&& __os, const _Tp& __x) { __os << __x; return _VSTD::move(__os); diff --git a/libcxx/test/std/input.output/iostream.format/input.streams/istream.rvalue/not_istreamable.verify.cpp b/libcxx/test/std/input.output/iostream.format/input.streams/istream.rvalue/not_istreamable.verify.cpp new file mode 100644 index 000000000000..1a03ed69b140 --- /dev/null +++ b/libcxx/test/std/input.output/iostream.format/input.streams/istream.rvalue/not_istreamable.verify.cpp @@ -0,0 +1,20 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03 + +// Make sure the rvalue overload of operator>> isn't part of the overload set +// when the type is not input streamable from a lvalue stream. + +#include +#include + +struct Foo { }; + +using X = decltype(std::declval() >> std::declval()); // expected-error {{invalid operands to binary expression}} +using Y = decltype(std::declval() >> std::declval()); // expected-error {{invalid operands to binary expression}} diff --git a/libcxx/test/std/input.output/iostream.format/input.streams/istream.rvalue/rvalue.pass.cpp b/libcxx/test/std/input.output/iostream.format/input.streams/istream.rvalue/rvalue.pass.cpp index df65aed76dfd..70ada3a9239f 100644 --- a/libcxx/test/std/input.output/iostream.format/input.streams/istream.rvalue/rvalue.pass.cpp +++ b/libcxx/test/std/input.output/iostream.format/input.streams/istream.rvalue/rvalue.pass.cpp @@ -10,9 +10,8 @@ // -// template -// basic_istream& -// operator>>(basic_istream&& is, T&& x); +// template +// Stream&& operator>>(Stream&& is, T&& x); #include #include @@ -44,32 +43,44 @@ public: CharT* egptr() const {return base::egptr();} }; +struct Int { + int value; + template + friend void operator>>(std::basic_istream& is, Int& self) { + is >> self.value; + } +}; -struct A{}; +struct A { }; bool called = false; -void operator>>(std::istream&, A&&){ called = true; } +void operator>>(std::istream&, A&&) { called = true; } int main(int, char**) { { testbuf sb(" 123"); - int i = 0; - std::istream(&sb) >> i; - assert(i == 123); + Int i = {0}; + std::istream is(&sb); + std::istream&& result = (std::move(is) >> i); + assert(&result == &is); + assert(i.value == 123); } { testbuf sb(L" 123"); - int i = 0; - std::wistream(&sb) >> i; - assert(i == 123); + Int i = {0}; + std::wistream is(&sb); + std::wistream&& result = (std::move(is) >> i); + assert(&result == &is); + assert(i.value == 123); } - { // test perfect forwarding + { + // test perfect forwarding assert(called == false); std::istringstream ss; - auto&& out = (std::move(ss) >> A{}); - assert(&out == &ss); + std::istringstream&& result = (std::move(ss) >> A{}); + assert(&result == &ss); assert(called); } - return 0; + return 0; } diff --git a/libcxx/test/std/input.output/iostream.format/output.streams/ostream.rvalue/not_ostreamable.verify.cpp b/libcxx/test/std/input.output/iostream.format/output.streams/ostream.rvalue/not_ostreamable.verify.cpp new file mode 100644 index 000000000000..7ca36c562261 --- /dev/null +++ b/libcxx/test/std/input.output/iostream.format/output.streams/ostream.rvalue/not_ostreamable.verify.cpp @@ -0,0 +1,19 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03 + +// Make sure the rvalue overload of operator<< isn't part of the overload set +// when the type is not output streamable into a lvalue stream. + +#include +#include + +struct Foo { }; + +using X = decltype(std::declval() << std::declval()); // expected-error {{invalid operands to binary expression}} diff --git a/libcxx/test/std/input.output/iostream.format/output.streams/ostream.rvalue/CharT_pointer.pass.cpp b/libcxx/test/std/input.output/iostream.format/output.streams/ostream.rvalue/rvalue.pass.cpp similarity index 71% rename from libcxx/test/std/input.output/iostream.format/output.streams/ostream.rvalue/CharT_pointer.pass.cpp rename to libcxx/test/std/input.output/iostream.format/output.streams/ostream.rvalue/rvalue.pass.cpp index 87ab6bae3907..99e70c425169 100644 --- a/libcxx/test/std/input.output/iostream.format/output.streams/ostream.rvalue/CharT_pointer.pass.cpp +++ b/libcxx/test/std/input.output/iostream.format/output.streams/ostream.rvalue/rvalue.pass.cpp @@ -10,12 +10,8 @@ // -// template > -// class basic_ostream; - -// template -// basic_ostream& -// operator<<(basic_ostream&& os, const T& x); +// template +// Stream&& operator<<(Stream&& os, const T& x); #include #include @@ -55,19 +51,32 @@ protected: } }; +struct Int { + int value; + template + friend void operator<<(std::basic_ostream& os, Int const& self) { + os << self.value; + } +}; int main(int, char**) { { testbuf sb; - std::ostream(&sb) << "testing..."; - assert(sb.str() == "testing..."); + std::ostream os(&sb); + Int const i = {123}; + std::ostream&& result = (std::move(os) << i); + assert(&result == &os); + assert(sb.str() == "123"); } { testbuf sb; - std::wostream(&sb) << L"123"; + std::wostream os(&sb); + Int const i = {123}; + std::wostream&& result = (std::move(os) << i); + assert(&result == &os); assert(sb.str() == L"123"); } - return 0; + return 0; } diff --git a/libcxx/www/cxx2a_status.html b/libcxx/www/cxx2a_status.html index d1bf4744b495..25d9f24a001f 100644 --- a/libcxx/www/cxx2a_status.html +++ b/libcxx/www/cxx2a_status.html @@ -468,7 +468,7 @@ 3149DefaultConstructible should require default initializationBelfast --> - 1203More useful rvalue stream insertionPrague + 1203More useful rvalue stream insertionPrague12.0 2859Definition of reachable in [ptr.launder] misses pointer arithmetic from pointer-interconvertible objectPrague 3018shared_ptr of function typePrague 3050Conversion specification problem in chrono::duration constructorPrague