From fbcedacca60fefc99ffcc94d8711edc12dcc70e5 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Wed, 17 Mar 2010 00:22:56 +0000 Subject: [PATCH] re PR libstdc++/43397 (std::function can't forward rvalue reference for pointer to member function) 2010-03-17 Jonathan Wakely PR libstdc++/43397 * include/std/functional (_Mem_fn): Use perfect forwarding. * testsuite/20_util/function/43397.cc: New. From-SVN: r157504 --- libstdc++-v3/ChangeLog | 6 ++ libstdc++-v3/include/std/functional | 52 ++++++++----- .../testsuite/20_util/function/43397.cc | 78 +++++++++++++++++++ 3 files changed, 116 insertions(+), 20 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/function/43397.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 8e4842e1a72..92512f52c55 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,9 @@ +2010-03-17 Jonathan Wakely + + PR libstdc++/43397 + * include/std/functional (_Mem_fn): Use perfect forwarding. + * testsuite/20_util/function/43397.cc: New. + 2010-03-16 Paolo Carlini PR libstdc++/43394 diff --git a/libstdc++-v3/include/std/functional b/libstdc++-v3/include/std/functional index 7922a7d319b..2ba7243837a 100644 --- a/libstdc++-v3/include/std/functional +++ b/libstdc++-v3/include/std/functional @@ -500,12 +500,12 @@ namespace std _Res _M_call(_Tp& __object, const volatile _Class *, _ArgTypes... __args) const - { return (__object.*__pmf)(__args...); } + { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); } template _Res _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const - { return ((*__ptr).*__pmf)(__args...); } + { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); } public: typedef _Res result_type; @@ -515,18 +515,21 @@ namespace std // Handle objects _Res operator()(_Class& __object, _ArgTypes... __args) const - { return (__object.*__pmf)(__args...); } + { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); } // Handle pointers _Res operator()(_Class* __object, _ArgTypes... __args) const - { return (__object->*__pmf)(__args...); } + { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); } // Handle smart pointers, references and pointers to derived template _Res operator()(_Tp& __object, _ArgTypes... __args) const - { return _M_call(__object, &__object, __args...); } + { + return _M_call(__object, &__object, + std::forward<_ArgTypes>(__args)...); + } private: _Functor __pmf; @@ -544,12 +547,12 @@ namespace std _Res _M_call(_Tp& __object, const volatile _Class *, _ArgTypes... __args) const - { return (__object.*__pmf)(__args...); } + { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); } template _Res _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const - { return ((*__ptr).*__pmf)(__args...); } + { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); } public: typedef _Res result_type; @@ -559,17 +562,20 @@ namespace std // Handle objects _Res operator()(const _Class& __object, _ArgTypes... __args) const - { return (__object.*__pmf)(__args...); } + { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); } // Handle pointers _Res operator()(const _Class* __object, _ArgTypes... __args) const - { return (__object->*__pmf)(__args...); } + { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); } // Handle smart pointers, references and pointers to derived template _Res operator()(_Tp& __object, _ArgTypes... __args) const - { return _M_call(__object, &__object, __args...); } + { + return _M_call(__object, &__object, + std::forward<_ArgTypes>(__args)...); + } private: _Functor __pmf; @@ -587,12 +593,12 @@ namespace std _Res _M_call(_Tp& __object, const volatile _Class *, _ArgTypes... __args) const - { return (__object.*__pmf)(__args...); } + { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); } template _Res _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const - { return ((*__ptr).*__pmf)(__args...); } + { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); } public: typedef _Res result_type; @@ -602,18 +608,21 @@ namespace std // Handle objects _Res operator()(volatile _Class& __object, _ArgTypes... __args) const - { return (__object.*__pmf)(__args...); } + { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); } // Handle pointers _Res operator()(volatile _Class* __object, _ArgTypes... __args) const - { return (__object->*__pmf)(__args...); } + { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); } // Handle smart pointers, references and pointers to derived template _Res operator()(_Tp& __object, _ArgTypes... __args) const - { return _M_call(__object, &__object, __args...); } + { + return _M_call(__object, &__object, + std::forward<_ArgTypes>(__args)...); + } private: _Functor __pmf; @@ -631,12 +640,12 @@ namespace std _Res _M_call(_Tp& __object, const volatile _Class *, _ArgTypes... __args) const - { return (__object.*__pmf)(__args...); } + { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); } template _Res _M_call(_Tp& __ptr, const volatile void *, _ArgTypes... __args) const - { return ((*__ptr).*__pmf)(__args...); } + { return ((*__ptr).*__pmf)(std::forward<_ArgTypes>(__args)...); } public: typedef _Res result_type; @@ -646,17 +655,20 @@ namespace std // Handle objects _Res operator()(const volatile _Class& __object, _ArgTypes... __args) const - { return (__object.*__pmf)(__args...); } + { return (__object.*__pmf)(std::forward<_ArgTypes>(__args)...); } // Handle pointers _Res operator()(const volatile _Class* __object, _ArgTypes... __args) const - { return (__object->*__pmf)(__args...); } + { return (__object->*__pmf)(std::forward<_ArgTypes>(__args)...); } // Handle smart pointers, references and pointers to derived template _Res operator()(_Tp& __object, _ArgTypes... __args) const - { return _M_call(__object, &__object, __args...); } + { + return _M_call(__object, &__object, + std::forward<_ArgTypes>(__args)...); + } private: _Functor __pmf; diff --git a/libstdc++-v3/testsuite/20_util/function/43397.cc b/libstdc++-v3/testsuite/20_util/function/43397.cc new file mode 100644 index 00000000000..d76a27f3404 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/function/43397.cc @@ -0,0 +1,78 @@ +// { dg-options "-std=gnu++0x" } +// Copyright (C) 2010 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 +// . + +// 20.8.15 polymorphic function object wrapper + +#include +#include + +struct Foo +{ + Foo() { } + short operator() ( int && ) { return 1; } + short operator() ( int && ) const { return 2; } + short operator() ( int && ) volatile { return 3; } + short operator() ( int && ) const volatile { return 4; } + short func( int && ) { return 5; } + short func_c( int && ) const { return 6; } + short func_v( int && ) volatile { return 7; } + short func_cv( int && ) const volatile { return 8; } +}; + +void test01() +{ + bool test __attribute__((unused)) = true; + + using std::function; + using std::ref; + + Foo foo; + Foo const foo_c; + Foo volatile foo_v; + Foo const volatile foo_cv; + + std::function< int ( int && ) > f1( ref(foo) ); + VERIFY( f1(0) == 1 ); + + std::function< int ( int && ) > f2( ref(foo_c) ); + VERIFY( f2(0) == 2 ); + + std::function< int ( int && ) > f3( ref(foo_v) ); + VERIFY( f3(0) == 3 ); + + std::function< int ( int && ) > f4( ref(foo_cv) ); + VERIFY( f4(0) == 4 ); + + std::function< int ( Foo &, int && ) > f5( &Foo::func ) ; + VERIFY( f5(foo, 0) == 5 ); + + std::function< int ( Foo const &, int && ) > f6( &Foo::func_c ) ; + VERIFY( f6(foo_c, 0) == 6 ); + + std::function< int ( Foo volatile &, int && ) > f7( &Foo::func_v ) ; + VERIFY( f7(foo_v, 0) == 7 ); + + std::function< int ( Foo const volatile &, int && ) > f8( &Foo::func_cv ) ; + VERIFY( f8(foo_cv, 0) == 8 ); +} + +int main() +{ + test01(); + return 0; +}