From 00677c48777bca36afe6fcddd380d004959998a3 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Thu, 14 Dec 2017 17:18:22 +0000 Subject: [PATCH] PR libstdc++/83427 detect weak result type from noexcept functions PR libstdc++/83427 * include/bits/refwrap.h (_Maybe_unary_or_binary_function): Move here from . (_Mem_fn_traits_base, _Mem_fn_traits): Move here, from . (_Weak_result_type_impl, _Reference_wrapper_base): Deduce noexcept for function types. Remove partial specializations for member functions. (_Weak_result_type_impl): Remove unused partial specializations for non-referenceable function types and for references to functions. (_Weak_result_type_memfun, _Reference_wrapper_base_memfun): New helpers to handle member functions via _Mem_fn_traits. (_Weak_result_type, reference_wrapper): Derive from new helpers. * include/bits/std_function.h (_Maybe_unary_or_binary_function): Move to . * include/std/functional (_Pack, _AllConvertible, _NotSame): Remove. (_Mem_fn_traits_base, _Mem_fn_traits): Move to . * testsuite/20_util/bind/83427.cc: New test. * testsuite/20_util/bind/refqual.cc: Add noexcept to functions and check for weak result types. * testsuite/20_util/reference_wrapper/83427.cc: New test. From-SVN: r255651 --- libstdc++-v3/ChangeLog | 21 ++ libstdc++-v3/include/bits/refwrap.h | 242 ++++++++---------- libstdc++-v3/include/bits/std_function.h | 19 -- libstdc++-v3/include/std/functional | 65 +---- libstdc++-v3/testsuite/20_util/bind/83427.cc | 31 +++ .../testsuite/20_util/bind/refqual.cc | 8 +- .../20_util/reference_wrapper/83427.cc | 39 +++ 7 files changed, 209 insertions(+), 216 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/bind/83427.cc create mode 100644 libstdc++-v3/testsuite/20_util/reference_wrapper/83427.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index e035302bed4..143739f1ce2 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,26 @@ 2017-12-14 Jonathan Wakely + PR libstdc++/83427 + * include/bits/refwrap.h (_Maybe_unary_or_binary_function): Move here + from . + (_Mem_fn_traits_base, _Mem_fn_traits): Move here, from . + (_Weak_result_type_impl, _Reference_wrapper_base): Deduce noexcept + for function types. Remove partial specializations for member + functions. + (_Weak_result_type_impl): Remove unused partial specializations for + non-referenceable function types and for references to functions. + (_Weak_result_type_memfun, _Reference_wrapper_base_memfun): New + helpers to handle member functions via _Mem_fn_traits. + (_Weak_result_type, reference_wrapper): Derive from new helpers. + * include/bits/std_function.h (_Maybe_unary_or_binary_function): Move + to . + * include/std/functional (_Pack, _AllConvertible, _NotSame): Remove. + (_Mem_fn_traits_base, _Mem_fn_traits): Move to . + * testsuite/20_util/bind/83427.cc: New test. + * testsuite/20_util/bind/refqual.cc: Add noexcept to functions and + check for weak result types. + * testsuite/20_util/reference_wrapper/83427.cc: New test. + * testsuite/26_numerics/complex/inserters_extractors/char/dr2714.cc: Add tests using noskipws. diff --git a/libstdc++-v3/include/bits/refwrap.h b/libstdc++-v3/include/bits/refwrap.h index 5e5b61060e3..1b64cd8fbf5 100644 --- a/libstdc++-v3/include/bits/refwrap.h +++ b/libstdc++-v3/include/bits/refwrap.h @@ -44,6 +44,69 @@ namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION + /** + * Derives from @c unary_function or @c binary_function, or perhaps + * nothing, depending on the number of arguments provided. The + * primary template is the basis case, which derives nothing. + */ + template + struct _Maybe_unary_or_binary_function { }; + + /// Derives from @c unary_function, as appropriate. + template + struct _Maybe_unary_or_binary_function<_Res, _T1> + : std::unary_function<_T1, _Res> { }; + + /// Derives from @c binary_function, as appropriate. + template + struct _Maybe_unary_or_binary_function<_Res, _T1, _T2> + : std::binary_function<_T1, _T2, _Res> { }; + + template + struct _Mem_fn_traits; + + template + struct _Mem_fn_traits_base + { + using __result_type = _Res; + using __maybe_type + = _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>; + using __arity = integral_constant; + }; + +#define _GLIBCXX_MEM_FN_TRAITS2(_CV, _REF, _LVAL, _RVAL) \ + template \ + struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes...) _CV _REF> \ + : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \ + { \ + using __vararg = false_type; \ + }; \ + template \ + struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes... ...) _CV _REF> \ + : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \ + { \ + using __vararg = true_type; \ + }; + +#define _GLIBCXX_MEM_FN_TRAITS(_REF, _LVAL, _RVAL) \ + _GLIBCXX_MEM_FN_TRAITS2( , _REF, _LVAL, _RVAL) \ + _GLIBCXX_MEM_FN_TRAITS2(const , _REF, _LVAL, _RVAL) \ + _GLIBCXX_MEM_FN_TRAITS2(volatile , _REF, _LVAL, _RVAL) \ + _GLIBCXX_MEM_FN_TRAITS2(const volatile, _REF, _LVAL, _RVAL) + +_GLIBCXX_MEM_FN_TRAITS( , true_type, true_type) +_GLIBCXX_MEM_FN_TRAITS(&, true_type, false_type) +_GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type) + +#if __cplusplus > 201402L +_GLIBCXX_MEM_FN_TRAITS(noexcept, true_type, true_type) +_GLIBCXX_MEM_FN_TRAITS(& noexcept, true_type, false_type) +_GLIBCXX_MEM_FN_TRAITS(&& noexcept, false_type, true_type) +#endif + +#undef _GLIBCXX_MEM_FN_TRAITS +#undef _GLIBCXX_MEM_FN_TRAITS2 + /// If we have found a result_type, extract it. template> struct _Maybe_get_result_type @@ -64,101 +127,52 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { }; /// Retrieve the result type for a function type. - template - struct _Weak_result_type_impl<_Res(_ArgTypes...)> - { typedef _Res result_type; }; - - template - struct _Weak_result_type_impl<_Res(_ArgTypes......)> - { typedef _Res result_type; }; - - template - struct _Weak_result_type_impl<_Res(_ArgTypes...) const> + template + struct _Weak_result_type_impl<_Res(_ArgTypes...) _GLIBCXX_NOEXCEPT_QUAL> { typedef _Res result_type; }; - template - struct _Weak_result_type_impl<_Res(_ArgTypes......) const> - { typedef _Res result_type; }; - - template - struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile> - { typedef _Res result_type; }; - - template - struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile> - { typedef _Res result_type; }; - - template - struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile> - { typedef _Res result_type; }; - - template - struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile> - { typedef _Res result_type; }; - - /// Retrieve the result type for a function reference. - template - struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)> - { typedef _Res result_type; }; - - template - struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)> + /// Retrieve the result type for a varargs function type. + template + struct _Weak_result_type_impl<_Res(_ArgTypes......) _GLIBCXX_NOEXCEPT_QUAL> { typedef _Res result_type; }; /// Retrieve the result type for a function pointer. - template - struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)> - { typedef _Res result_type; }; - - template - struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)> + template + struct _Weak_result_type_impl<_Res(*)(_ArgTypes...) _GLIBCXX_NOEXCEPT_QUAL> { typedef _Res result_type; }; - /// Retrieve result type for a member function pointer. - template - struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)> + /// Retrieve the result type for a varargs function pointer. + template + struct + _Weak_result_type_impl<_Res(*)(_ArgTypes......) _GLIBCXX_NOEXCEPT_QUAL> { typedef _Res result_type; }; - template - struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)> - { typedef _Res result_type; }; - - /// Retrieve result type for a const member function pointer. - template - struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const> - { typedef _Res result_type; }; - - template - struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const> - { typedef _Res result_type; }; - - /// Retrieve result type for a volatile member function pointer. - template - struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile> - { typedef _Res result_type; }; - - template - struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile> - { typedef _Res result_type; }; + // Let _Weak_result_type_impl perform the real work. + template::value> + struct _Weak_result_type_memfun + : _Weak_result_type_impl<_Functor> + { }; - /// Retrieve result type for a const volatile member function pointer. - template - struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) - const volatile> - { typedef _Res result_type; }; + // A pointer to member function has a weak result type. + template + struct _Weak_result_type_memfun<_MemFunPtr, true> + { + using result_type = typename _Mem_fn_traits<_MemFunPtr>::__result_type; + }; - template - struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) - const volatile> - { typedef _Res result_type; }; + // A pointer to data member doesn't have a weak result type. + template + struct _Weak_result_type_memfun<_Func _Class::*, false> + { }; /** * Strip top-level cv-qualifiers from the function object and let - * _Weak_result_type_impl perform the real work. + * _Weak_result_type_memfun perform the real work. */ template struct _Weak_result_type - : _Weak_result_type_impl::type> + : _Weak_result_type_memfun::type> { }; // Detect nested argument_type. @@ -201,8 +215,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { }; // - a function type (unary) - template - struct _Reference_wrapper_base<_Res(_T1)> + template + struct _Reference_wrapper_base<_Res(_T1) _GLIBCXX_NOEXCEPT_QUAL> : unary_function<_T1, _Res> { }; @@ -222,8 +236,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { }; // - a function type (binary) - template - struct _Reference_wrapper_base<_Res(_T1, _T2)> + template + struct _Reference_wrapper_base<_Res(_T1, _T2) _GLIBCXX_NOEXCEPT_QUAL> : binary_function<_T1, _T2, _Res> { }; @@ -243,64 +257,28 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { }; // - a function pointer type (unary) - template - struct _Reference_wrapper_base<_Res(*)(_T1)> + template + struct _Reference_wrapper_base<_Res(*)(_T1) _GLIBCXX_NOEXCEPT_QUAL> : unary_function<_T1, _Res> { }; // - a function pointer type (binary) - template - struct _Reference_wrapper_base<_Res(*)(_T1, _T2)> + template + struct _Reference_wrapper_base<_Res(*)(_T1, _T2) _GLIBCXX_NOEXCEPT_QUAL> : binary_function<_T1, _T2, _Res> { }; - // - a pointer to member function type (unary, no qualifiers) - template - struct _Reference_wrapper_base<_Res (_T1::*)()> - : unary_function<_T1*, _Res> - { }; - - // - a pointer to member function type (binary, no qualifiers) - template - struct _Reference_wrapper_base<_Res (_T1::*)(_T2)> - : binary_function<_T1*, _T2, _Res> - { }; - - // - a pointer to member function type (unary, const) - template - struct _Reference_wrapper_base<_Res (_T1::*)() const> - : unary_function - { }; - - // - a pointer to member function type (binary, const) - template - struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const> - : binary_function - { }; - - // - a pointer to member function type (unary, volatile) - template - struct _Reference_wrapper_base<_Res (_T1::*)() volatile> - : unary_function - { }; - - // - a pointer to member function type (binary, volatile) - template - struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile> - : binary_function - { }; - - // - a pointer to member function type (unary, const volatile) - template - struct _Reference_wrapper_base<_Res (_T1::*)() const volatile> - : unary_function + template::value> + struct _Reference_wrapper_base_memfun + : _Reference_wrapper_base<_Tp> { }; - // - a pointer to member function type (binary, const volatile) - template - struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile> - : binary_function - { }; + template + struct _Reference_wrapper_base_memfun<_MemFunPtr, true> + : _Mem_fn_traits<_MemFunPtr>::__maybe_type + { + using result_type = typename _Mem_fn_traits<_MemFunPtr>::__result_type; + }; /** * @brief Primary class template for reference_wrapper. @@ -309,7 +287,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template class reference_wrapper - : public _Reference_wrapper_base::type> + : public _Reference_wrapper_base_memfun::type> { _Tp* _M_data; diff --git a/libstdc++-v3/include/bits/std_function.h b/libstdc++-v3/include/bits/std_function.h index a9ba7567b33..94b7ee99067 100644 --- a/libstdc++-v3/include/bits/std_function.h +++ b/libstdc++-v3/include/bits/std_function.h @@ -48,25 +48,6 @@ namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION - /** - * Derives from @c unary_function or @c binary_function, or perhaps - * nothing, depending on the number of arguments provided. The - * primary template is the basis case, which derives nothing. - */ - template - struct _Maybe_unary_or_binary_function { }; - - /// Derives from @c unary_function, as appropriate. - template - struct _Maybe_unary_or_binary_function<_Res, _T1> - : std::unary_function<_T1, _Res> { }; - - /// Derives from @c binary_function, as appropriate. - template - struct _Maybe_unary_or_binary_function<_Res, _T1, _T2> - : std::binary_function<_T1, _T2, _Res> { }; - - /** * @brief Exception class thrown when class template function's * operator() is called with an empty target. diff --git a/libstdc++-v3/include/std/functional b/libstdc++-v3/include/std/functional index a6d121bb150..f94c63b213e 100644 --- a/libstdc++-v3/include/std/functional +++ b/libstdc++-v3/include/std/functional @@ -55,7 +55,8 @@ #include #include #include -#include +#include // std::reference_wrapper and _Mem_fn_traits +#include // std::function #if __cplusplus > 201402L # include # include @@ -82,68 +83,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } #endif - template - struct _Pack : integral_constant - { }; - - template - struct _AllConvertible : false_type - { }; - - template - struct _AllConvertible<_Pack<_From...>, _Pack<_To...>, true> - : __and_...> - { }; - - template - using _NotSame = __not_::type, - typename std::decay<_Tp2>::type>>; - - template - struct _Mem_fn_traits; - - template - struct _Mem_fn_traits_base - { - using __result_type = _Res; - using __maybe_type - = _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>; - using __arity = integral_constant; - }; - -#define _GLIBCXX_MEM_FN_TRAITS2(_CV, _REF, _LVAL, _RVAL) \ - template \ - struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes...) _CV _REF> \ - : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \ - { \ - using __vararg = false_type; \ - }; \ - template \ - struct _Mem_fn_traits<_Res (_Class::*)(_ArgTypes... ...) _CV _REF> \ - : _Mem_fn_traits_base<_Res, _CV _Class, _ArgTypes...> \ - { \ - using __vararg = true_type; \ - }; - -#define _GLIBCXX_MEM_FN_TRAITS(_REF, _LVAL, _RVAL) \ - _GLIBCXX_MEM_FN_TRAITS2( , _REF, _LVAL, _RVAL) \ - _GLIBCXX_MEM_FN_TRAITS2(const , _REF, _LVAL, _RVAL) \ - _GLIBCXX_MEM_FN_TRAITS2(volatile , _REF, _LVAL, _RVAL) \ - _GLIBCXX_MEM_FN_TRAITS2(const volatile, _REF, _LVAL, _RVAL) - -_GLIBCXX_MEM_FN_TRAITS( , true_type, true_type) -_GLIBCXX_MEM_FN_TRAITS(&, true_type, false_type) -_GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type) - -#if __cplusplus > 201402L -_GLIBCXX_MEM_FN_TRAITS(noexcept, true_type, true_type) -_GLIBCXX_MEM_FN_TRAITS(& noexcept, true_type, false_type) -_GLIBCXX_MEM_FN_TRAITS(&& noexcept, false_type, true_type) -#endif - -#undef _GLIBCXX_MEM_FN_TRAITS -#undef _GLIBCXX_MEM_FN_TRAITS2 - template::value> class _Mem_fn_base diff --git a/libstdc++-v3/testsuite/20_util/bind/83427.cc b/libstdc++-v3/testsuite/20_util/bind/83427.cc new file mode 100644 index 00000000000..e96841435f2 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/bind/83427.cc @@ -0,0 +1,31 @@ +// Copyright (C) 2017 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++17" } +// { dg-do compile { target c++17 } } + +#include + +// PR libstdc++/83427 + +int f() noexcept { return 0; } +auto b = std::bind(f); +static_assert(std::is_same_v); + +struct X { long f() const & noexcept { return 0L; } }; +auto b2 = std::bind(&X::f, X{}); +static_assert(std::is_same_v); diff --git a/libstdc++-v3/testsuite/20_util/bind/refqual.cc b/libstdc++-v3/testsuite/20_util/bind/refqual.cc index e7088746607..09fe6d52d37 100644 --- a/libstdc++-v3/testsuite/20_util/bind/refqual.cc +++ b/libstdc++-v3/testsuite/20_util/bind/refqual.cc @@ -22,8 +22,8 @@ struct X { - int f() const& { return 0; } - int g(int i, ...)& { return i; } + int f() const& noexcept { return 0; } + int g(int i, ...)& noexcept { return i; } }; void @@ -34,6 +34,10 @@ test01() VERIFY( b() == 0 ); auto bb = std::bind(&X::g, &x, 1, 2); VERIFY( bb() == 1 ); + + // Check for weak result types: + using T1 = decltype(b)::result_type; + using T2 = decltype(bb)::result_type; } int diff --git a/libstdc++-v3/testsuite/20_util/reference_wrapper/83427.cc b/libstdc++-v3/testsuite/20_util/reference_wrapper/83427.cc new file mode 100644 index 00000000000..c4b360f0bb8 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/reference_wrapper/83427.cc @@ -0,0 +1,39 @@ +// Copyright (C) 2017 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++17" } +// { dg-do compile { target c++17 } } + +#include + +// PR libstdc++/83427 + +int f(short) noexcept { return 0; } +std::reference_wrapper r(f); +static_assert(std::is_same_v); +static_assert(std::is_same_v); + +auto* p = &f; +std::reference_wrapper r2(p); +static_assert(std::is_same_v); +static_assert(std::is_same_v); + +struct X { long f() const & noexcept { return 0L; } }; +auto m = &X::f; +std::reference_wrapper r3(m); +static_assert(std::is_same_v); +static_assert(std::is_same_v); -- 2.30.2