From a7da4881303a7bc9a59014f0d03cd946a9cdecec Mon Sep 17 00:00:00 2001 From: Ville Voutilainen Date: Wed, 30 Nov 2016 11:59:50 +0200 Subject: Implement LWG 2534, Constrain rvalue stream operators. * include/std/istream (__is_convertible_to_basic_istream): New. (__is_extractable): Likewise. (operator>>(basic_istream<_CharT, _Traits>&&, _Tp&&)): Turn the stream parameter into a template parameter and constrain. * include/std/ostream (__is_convertible_to_basic_ostream): New. (__is_insertable): Likewise. (operator<<(basic_ostream<_CharT, _Traits>&&, const _Tp&)): Turn the stream parameter into a template parameter and constrain. * testsuite/27_io/basic_istream/extractors_other/char/4.cc: New. * testsuite/27_io/basic_istream/extractors_other/wchar_t/4.cc: Likewise. * testsuite/27_io/basic_ostream/inserters_other/char/6.cc: Likewise. * testsuite/27_io/basic_ostream/inserters_other/wchar_t/6.cc: Likewise. From-SVN: r243006 --- libstdc++-v3/include/std/istream | 33 ++++++++++++++++++++++++++++++--- libstdc++-v3/include/std/ostream | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 6 deletions(-) (limited to 'libstdc++-v3/include/std') diff --git a/libstdc++-v3/include/std/istream b/libstdc++-v3/include/std/istream index c8a2e08..4f0e940 100644 --- a/libstdc++-v3/include/std/istream +++ b/libstdc++-v3/include/std/istream @@ -908,6 +908,28 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ws(basic_istream<_CharT, _Traits>& __is); #if __cplusplus >= 201103L + + template + struct __is_convertible_to_basic_istream + { + template + static true_type __check(basic_istream<_Ch, _Up>*); + + static false_type __check(void*); + public: + using type = decltype(__check(declval<_Tp*>())); + constexpr static bool value = type::value; + }; + + template + struct __is_extractable : false_type {}; + + template + struct __is_extractable<_Istream, _Tp, + __void_t() + >> declval<_Tp>())>> + : true_type {}; + // [27.7.1.6] Rvalue stream extraction // _GLIBCXX_RESOLVE_LIB_DEFECTS // 2328. Rvalue stream extraction should use perfect forwarding @@ -921,9 +943,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * rvalue streams since they won't bind to the extractor functions * that take an lvalue reference. */ - template - inline basic_istream<_CharT, _Traits>& - operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp&& __x) + template + inline + typename enable_if<__and_<__not_>, + __is_convertible_to_basic_istream< + typename remove_reference<_Istream>::type>, + __is_extractable<_Istream&, _Tp&&>>::value, + _Istream&>::type + operator>>(_Istream&& __is, _Tp&& __x) { __is >> std::forward<_Tp>(__x); return __is; diff --git a/libstdc++-v3/include/std/ostream b/libstdc++-v3/include/std/ostream index 0bf53f0..a1fe892 100644 --- a/libstdc++-v3/include/std/ostream +++ b/libstdc++-v3/include/std/ostream @@ -613,6 +613,27 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { return __os.flush(); } #if __cplusplus >= 201103L + template + struct __is_convertible_to_basic_ostream + { + template + static true_type __check(basic_ostream<_Ch, _Up>*); + + static false_type __check(void*); + public: + using type = decltype(__check(declval<_Tp*>())); + constexpr static bool value = type::value; + }; + + template + struct __is_insertable : false_type {}; + + template + struct __is_insertable<_Ostream, _Tp, + __void_t() + << declval())>> + : true_type {}; + /** * @brief Generic inserter for rvalue stream * @param __os An input stream. @@ -623,9 +644,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * rvalue streams since they won't bind to the inserter functions * that take an lvalue reference. */ - template - inline basic_ostream<_CharT, _Traits>& - operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x) + template + inline + typename enable_if<__and_<__not_>, + __is_convertible_to_basic_ostream< + typename remove_reference<_Ostream>::type>, + __is_insertable<_Ostream&, const _Tp&>>::value, + _Ostream&>::type + //basic_ostream<_CharT, _Traits>& + operator<<(_Ostream&& __os, const _Tp& __x) { __os << __x; return __os; -- cgit v1.1