From patchwork Mon Nov 26 23:51:09 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 202045 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id D3C132C0085 for ; Tue, 27 Nov 2012 10:51:30 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1354578692; h=Comment: DomainKey-Signature:Received:Received:Received:Received: MIME-Version:Received:Received:Date:Message-ID:Subject:From:To: Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=vf0RKY8 L49KSmo8dDvtOcDdOJ5I=; b=FRSm4io2+qrXeCtab4FuSHnTDlkO25wPjN9MUup j2fuwj66oAJDa1/s+jdNUDnQ6P1bb8YBcw9UOBpDGOBbbpat8wXd/fmlnhmS4x3h Q2lEfH/icZcoXCWe7rCRIRJ8VxG8mfHBoWWND8F5lhSHhCACR3gbRAdaDAomuAXS rHKI= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:MIME-Version:Received:Received:Date:Message-ID:Subject:From:To:Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=rMKr0Je/oYj7DygXfjY3G5zrNqPlUB55r1vcvVQnnXrUctC+GuE406GpxkkwNQ qe/8bjnAMF613Cj4fiMVB5pecovJfTa+a8M7J3lyy3HiqgeOtZETZ1bRkw05ipnf 3xedbaMXqS0vxCRKgI7ySsgCBdU0RbR33aZBXR/T3Zmdc=; Received: (qmail 20450 invoked by alias); 26 Nov 2012 23:51:21 -0000 Received: (qmail 20433 invoked by uid 22791); 26 Nov 2012 23:51:20 -0000 X-SWARE-Spam-Status: No, hits=-4.5 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FILL_THIS_FORM, FREEMAIL_FROM, KHOP_RCVD_TRUST, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE X-Spam-Check-By: sourceware.org Received: from mail-ie0-f175.google.com (HELO mail-ie0-f175.google.com) (209.85.223.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 26 Nov 2012 23:51:11 +0000 Received: by mail-ie0-f175.google.com with SMTP id qd14so12220994ieb.20 for ; Mon, 26 Nov 2012 15:51:10 -0800 (PST) MIME-Version: 1.0 Received: by 10.50.12.138 with SMTP id y10mr16075011igb.58.1353973869845; Mon, 26 Nov 2012 15:51:09 -0800 (PST) Received: by 10.42.158.202 with HTTP; Mon, 26 Nov 2012 15:51:09 -0800 (PST) Date: Mon, 26 Nov 2012 23:51:09 +0000 Message-ID: Subject: [patch] fix libstdc++/55463 calling mem_fn with rvalues From: Jonathan Wakely To: "libstdc++" , gcc-patches Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org PR libstdc++/55463 * include/std/functional (_Mem_fn): Handle rvalue objects. Add noexcept-specifications. * testsuite/20_util/function_objects/mem_fn/55463.cc: New. * testsuite/20_util/bind/ref_neg.cc: Adjust dg-error line numbers. Tested x86_64-linux, committed to trunk. commit 4ddc9752bc93b233efcff9273b0fe4e488a783b8 Author: Jonathan Wakely Date: Sun Nov 25 21:56:23 2012 +0000 PR libstdc++/55463 * include/std/functional (_Mem_fn): Handle rvalue objects. Add noexcept-specifications. * testsuite/20_util/function_objects/mem_fn/55463.cc: New. * testsuite/20_util/bind/ref_neg.cc: Adjust dg-error line numbers. diff --git a/libstdc++-v3/include/std/functional b/libstdc++-v3/include/std/functional index 4d6e6a8..1a98127 100644 --- a/libstdc++-v3/include/std/functional +++ b/libstdc++-v3/include/std/functional @@ -65,7 +65,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION class _Mem_fn; template _Mem_fn<_Tp _Class::*> - mem_fn(_Tp _Class::*); + mem_fn(_Tp _Class::*) noexcept; _GLIBCXX_HAS_NESTED_TYPE(result_type) @@ -528,13 +528,16 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) template _Res - _M_call(_Tp& __object, const volatile _Class *, + _M_call(_Tp&& __object, const volatile _Class *, _ArgTypes... __args) const - { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); } + { + return (std::forward<_Tp>(__object).*__pmf) + (std::forward<_ArgTypes>(__args)...); + } template _Res - _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const + _M_call(_Tp&& __ptr, const volatile void *, _ArgTypes... __args) const { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); } public: @@ -555,12 +558,17 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) // Handle smart pointers, references and pointers to derived template _Res - operator()(_Tp& __object, _ArgTypes... __args) const + operator()(_Tp&& __object, _ArgTypes... __args) const { - return _M_call(__object, &__object, + return _M_call(std::forward<_Tp>(__object), &__object, std::forward<_ArgTypes>(__args)...); } + template + _Res + operator()(reference_wrapper<_Tp> __ref, _ArgTypes... __args) const + { return operator()(__ref.get(), std::forward<_ArgTypes>(__args)...); } + private: _Functor __pmf; }; @@ -575,13 +583,16 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) template _Res - _M_call(_Tp& __object, const volatile _Class *, + _M_call(_Tp&& __object, const volatile _Class *, _ArgTypes... __args) const - { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); } + { + return (std::forward<_Tp>(__object).*__pmf) + (std::forward<_ArgTypes>(__args)...); + } template _Res - _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const + _M_call(_Tp&& __ptr, const volatile void *, _ArgTypes... __args) const { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); } public: @@ -601,12 +612,17 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) // Handle smart pointers, references and pointers to derived template - _Res operator()(_Tp& __object, _ArgTypes... __args) const + _Res operator()(_Tp&& __object, _ArgTypes... __args) const { - return _M_call(__object, &__object, + return _M_call(std::forward<_Tp>(__object), &__object, std::forward<_ArgTypes>(__args)...); } + template + _Res + operator()(reference_wrapper<_Tp> __ref, _ArgTypes... __args) const + { return operator()(__ref.get(), std::forward<_ArgTypes>(__args)...); } + private: _Functor __pmf; }; @@ -621,13 +637,16 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) template _Res - _M_call(_Tp& __object, const volatile _Class *, + _M_call(_Tp&& __object, const volatile _Class *, _ArgTypes... __args) const - { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); } + { + return (std::forward<_Tp>(__object).*__pmf) + (std::forward<_ArgTypes>(__args)...); + } template _Res - _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const + _M_call(_Tp&& __ptr, const volatile void *, _ArgTypes... __args) const { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); } public: @@ -648,12 +667,17 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) // Handle smart pointers, references and pointers to derived template _Res - operator()(_Tp& __object, _ArgTypes... __args) const + operator()(_Tp&& __object, _ArgTypes... __args) const { - return _M_call(__object, &__object, + return _M_call(std::forward<_Tp>(__object), &__object, std::forward<_ArgTypes>(__args)...); } + template + _Res + operator()(reference_wrapper<_Tp> __ref, _ArgTypes... __args) const + { return operator()(__ref.get(), std::forward<_ArgTypes>(__args)...); } + private: _Functor __pmf; }; @@ -668,13 +692,16 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) template _Res - _M_call(_Tp& __object, const volatile _Class *, + _M_call(_Tp&& __object, const volatile _Class *, _ArgTypes... __args) const - { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); } + { + return (std::forward<_Tp>(__object).*__pmf) + (std::forward<_ArgTypes>(__args)...); + } template _Res - _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const + _M_call(_Tp&& __ptr, const volatile void *, _ArgTypes... __args) const { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); } public: @@ -694,12 +721,17 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) // Handle smart pointers, references and pointers to derived template - _Res operator()(_Tp& __object, _ArgTypes... __args) const + _Res operator()(_Tp&& __object, _ArgTypes... __args) const { - return _M_call(__object, &__object, + return _M_call(std::forward<_Tp>(__object), &__object, std::forward<_ArgTypes>(__args)...); } + template + _Res + operator()(reference_wrapper<_Tp> __ref, _ArgTypes... __args) const + { return operator()(__ref.get(), std::forward<_ArgTypes>(__args)...); } + private: _Functor __pmf; }; @@ -720,91 +752,75 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) template class _Mem_fn<_Res _Class::*> { + using __pm_type = _Res _Class::*; + // This bit of genius is due to Peter Dimov, improved slightly by // Douglas Gregor. + // Made less elegant by Jonathan Wakely to support perfect forwarding. template - _Res& - _M_call(_Tp& __object, _Class *) const - { return __object.*__pm; } - - template - _Res& - _M_call(_Tp& __object, _Up * const *) const - { return (*__object).*__pm; } + auto + _M_call(_Tp&& __object, const _Class *) const noexcept + -> decltype(std::forward<_Tp>(__object).*std::declval<__pm_type&>()) + { return std::forward<_Tp>(__object).*__pm; } template - const _Res& - _M_call(_Tp& __object, const _Up * const *) const - { return (*__object).*__pm; } - - template - const _Res& - _M_call(_Tp& __object, const _Class *) const - { return __object.*__pm; } + auto + _M_call(_Tp&& __object, _Up * const *) const noexcept + -> decltype((*std::forward<_Tp>(__object)).*std::declval<__pm_type&>()) + { return (*std::forward<_Tp>(__object)).*__pm; } template - const _Res& - _M_call(_Tp& __ptr, const volatile void*) const + auto + _M_call(_Tp&& __ptr, const volatile void*) const + noexcept(noexcept((*__ptr).*std::declval<__pm_type&>())) + -> decltype((*__ptr).*std::declval<__pm_type&>()) { return (*__ptr).*__pm; } - template static _Tp& __get_ref(); - - template - static __sfinae_types::__one __check_const(_Tp&, _Class*); - template - static __sfinae_types::__one __check_const(_Tp&, _Up * const *); - template - static __sfinae_types::__two __check_const(_Tp&, const _Up * const *); - template - static __sfinae_types::__two __check_const(_Tp&, const _Class*); - template - static __sfinae_types::__two __check_const(_Tp&, const volatile void*); - public: - template - struct _Result_type - : _Mem_fn_const_or_non<_Res, - (sizeof(__sfinae_types::__two) - == sizeof(__check_const<_Tp>(__get_ref<_Tp>(), (_Tp*)0)))> - { }; - - template - struct result; - - template - struct result<_CVMem(_Tp)> - : public _Result_type<_Tp> { }; - - template - struct result<_CVMem(_Tp&)> - : public _Result_type<_Tp> { }; - explicit - _Mem_fn(_Res _Class::*__pm) : __pm(__pm) { } + _Mem_fn(_Res _Class::*__pm) noexcept : __pm(__pm) { } // Handle objects _Res& - operator()(_Class& __object) const + operator()(_Class& __object) const noexcept { return __object.*__pm; } const _Res& - operator()(const _Class& __object) const + operator()(const _Class& __object) const noexcept { return __object.*__pm; } + _Res&& + operator()(_Class&& __object) const noexcept + { return std::forward<_Class>(__object).*__pm; } + + const _Res&& + operator()(const _Class&& __object) const noexcept + { return std::forward(__object).*__pm; } + // Handle pointers _Res& - operator()(_Class* __object) const + operator()(_Class* __object) const noexcept { return __object->*__pm; } const _Res& - operator()(const _Class* __object) const + operator()(const _Class* __object) const noexcept { return __object->*__pm; } // Handle smart pointers and derived template - typename _Result_type<_Tp>::type - operator()(_Tp& __unknown) const - { return _M_call(__unknown, &__unknown); } + auto + operator()(_Tp&& __unknown) const + noexcept(noexcept(std::declval<_Mem_fn*>()->_M_call + (std::forward<_Tp>(__unknown), &__unknown))) + -> decltype(this->_M_call(std::forward<_Tp>(__unknown), &__unknown)) + { return _M_call(std::forward<_Tp>(__unknown), &__unknown); } + + template + auto + operator()(reference_wrapper<_Tp> __ref) const + noexcept(noexcept(std::declval<_Mem_fn&>()(__ref.get()))) + -> decltype((*this)(__ref.get())) + { return operator()(__ref.get()); } private: _Res _Class::*__pm; @@ -819,7 +835,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type) */ template inline _Mem_fn<_Tp _Class::*> - mem_fn(_Tp _Class::* __pm) + mem_fn(_Tp _Class::* __pm) noexcept { return _Mem_fn<_Tp _Class::*>(__pm); } diff --git a/libstdc++-v3/testsuite/20_util/bind/ref_neg.cc b/libstdc++-v3/testsuite/20_util/bind/ref_neg.cc index 0f1b4cf..c7f605e 100644 --- a/libstdc++-v3/testsuite/20_util/bind/ref_neg.cc +++ b/libstdc++-v3/testsuite/20_util/bind/ref_neg.cc @@ -30,10 +30,10 @@ void test01() { const int dummy = 0; std::bind(&inc, _1)(0); // { dg-error "no match" } - // { dg-error "rvalue|const" "" { target *-*-* } 1208 } - // { dg-error "rvalue|const" "" { target *-*-* } 1222 } - // { dg-error "rvalue|const" "" { target *-*-* } 1236 } - // { dg-error "rvalue|const" "" { target *-*-* } 1250 } + // { dg-error "rvalue|const" "" { target *-*-* } 1224 } + // { dg-error "rvalue|const" "" { target *-*-* } 1238 } + // { dg-error "rvalue|const" "" { target *-*-* } 1252 } + // { dg-error "rvalue|const" "" { target *-*-* } 1266 } std::bind(&inc, std::ref(dummy))(); // { dg-error "no match" } } diff --git a/libstdc++-v3/testsuite/20_util/function_objects/mem_fn/55463.cc b/libstdc++-v3/testsuite/20_util/function_objects/mem_fn/55463.cc new file mode 100644 index 0000000..5adce1b --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/function_objects/mem_fn/55463.cc @@ -0,0 +1,78 @@ +// { dg-options "-std=gnu++11" } +// { dg-do compile } + +// Copyright (C) 2012 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 +// . + +// PR libstdc++/55463 Passing rvalue objects to std::mem_fn + +#include + +struct X +{ + int& func(); + char& func_c() const; + short& func_v() volatile; + double& func_cv() const volatile; + + int data; +}; + +using CX = const X; + +struct smart_ptr +{ + X& operator*() const; +}; + +std::reference_wrapper ref(); +std::reference_wrapper cref(); + +void test01() +{ + int& i1 = std::mem_fn( &X::func )( X() ); + int& i2 = std::mem_fn( &X::func )( smart_ptr() ); + int& i3 = std::mem_fn( &X::func )( ref() ); + + char& c1 = std::mem_fn( &X::func_c )( X() ); + char& c2 = std::mem_fn( &X::func_c )( CX() ); + char& c3 = std::mem_fn( &X::func_c )( smart_ptr() ); + char& c4 = std::mem_fn( &X::func_c )( ref() ); + char& c5 = std::mem_fn( &X::func_c )( cref() ); + + short& s1 = std::mem_fn( &X::func_v )( X() ); + short& s2 = std::mem_fn( &X::func_v )( smart_ptr() ); + short& s3 = std::mem_fn( &X::func_v )( ref() ); + + double& d1 = std::mem_fn( &X::func_cv )( X() ); + double& d2 = std::mem_fn( &X::func_cv )( CX() ); + double& d3 = std::mem_fn( &X::func_cv )( smart_ptr() ); + double& d4 = std::mem_fn( &X::func_cv )( ref() ); + double& d5 = std::mem_fn( &X::func_cv )( cref() ); + + // [expr.mptr.oper] + // The result of a .* expression whose second operand is a pointer to a + // data member is of the same value category (3.10) as its first operand. + int&& rval = std::mem_fn( &X::data )( X() ); + const int&& crval = std::mem_fn( &X::data )( CX() ); + + int& sval = std::mem_fn( &X::data )( smart_ptr() ); + + int& val = std::mem_fn( &X::data )( ref() ); + const int& cval = std::mem_fn( &X::data )( cref() ); +} +