From 07c772ed4e2bfd648c705575b0ff7f0c06f44c0c Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Wed, 20 Jul 2016 19:21:53 +0100 Subject: [PATCH] LWG 2328 Rvalue stream extraction should use perfect forwarding * include/std/istream (operator>>(basic_istream&&, _Tp&)): Adjust to use perfect forwarding (LWG 2328). * testsuite/27_io/rvalue_streams.cc: Test perfect forwarding. * doc/xml/manual/intro.xml: Document DR 2328 status. From-SVN: r238533 --- libstdc++-v3/ChangeLog | 5 ++++ libstdc++-v3/doc/xml/manual/intro.xml | 6 +++++ libstdc++-v3/include/std/istream | 6 +++-- .../testsuite/27_io/rvalue_streams.cc | 24 +++++++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 40caba12448..ad31fe6fdee 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,10 @@ 2016-07-20 Jonathan Wakely + * include/std/istream (operator>>(basic_istream&&, _Tp&)): Adjust + to use perfect forwarding (LWG 2328). + * testsuite/27_io/rvalue_streams.cc: Test perfect forwarding. + * doc/xml/manual/intro.xml: Document DR 2328 status. + * libsupc++/pbase_type_info.cc (__pbase_type_info::__do_catch): Use static objects for catching nullptr as pointer to member types. diff --git a/libstdc++-v3/doc/xml/manual/intro.xml b/libstdc++-v3/doc/xml/manual/intro.xml index 7b836cddfbf..6335614efdb 100644 --- a/libstdc++-v3/doc/xml/manual/intro.xml +++ b/libstdc++-v3/doc/xml/manual/intro.xml @@ -948,6 +948,12 @@ requirements of the license of GCC. Update definitions of the partial specializations for const and volatile types. + 2328: + Rvalue stream extraction should use perfect forwarding + + Use perfect forwarding for right operand. + + 2329: regex_match()/regex_search() with match_results should forbid temporary strings diff --git a/libstdc++-v3/include/std/istream b/libstdc++-v3/include/std/istream index d4cf7bcbfd5..c8a2e08e9f9 100644 --- a/libstdc++-v3/include/std/istream +++ b/libstdc++-v3/include/std/istream @@ -909,6 +909,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #if __cplusplus >= 201103L // [27.7.1.6] Rvalue stream extraction + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 2328. Rvalue stream extraction should use perfect forwarding /** * @brief Generic extractor for rvalue stream * @param __is An input stream. @@ -921,9 +923,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template inline basic_istream<_CharT, _Traits>& - operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x) + operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp&& __x) { - __is >> __x; + __is >> std::forward<_Tp>(__x); return __is; } #endif // C++11 diff --git a/libstdc++-v3/testsuite/27_io/rvalue_streams.cc b/libstdc++-v3/testsuite/27_io/rvalue_streams.cc index eba5bc35961..5918595acbc 100644 --- a/libstdc++-v3/testsuite/27_io/rvalue_streams.cc +++ b/libstdc++-v3/testsuite/27_io/rvalue_streams.cc @@ -34,9 +34,33 @@ test01() VERIFY (i == i2); } +struct X { bool as_rvalue; }; + +void operator>>(std::istream&, X& x) { x.as_rvalue = false; } +void operator>>(std::istream&, X&& x) { x.as_rvalue = true; } + +// LWG 2328 Rvalue stream extraction should use perfect forwarding +void +test02() +{ + X x; + std::istringstream is; + auto& ref1 = (std::move(is) >> x); + VERIFY( &ref1 == &is ); + VERIFY( x.as_rvalue == false ); + auto& ref2 = (std::move(is) >> std::move(x)); + VERIFY( &ref2 == &is ); + VERIFY( x.as_rvalue == true ); + + char arr[2]; + std::istringstream("x") >> &arr[0]; + std::istringstream("x") >> arr; +} + int main() { test01(); + test02(); return 0; } -- 2.30.2