1// { dg-options "-std=gnu++11" } 2// { dg-do compile } 3 4// Copyright (C) 2012-2015 Free Software Foundation, Inc. 5// 6// This file is part of the GNU ISO C++ Library. This library is free 7// software; you can redistribute it and/or modify it under the 8// terms of the GNU General Public License as published by the 9// Free Software Foundation; either version 3, or (at your option) 10// any later version. 11 12// This library is distributed in the hope that it will be useful, 13// but WITHOUT ANY WARRANTY; without even the implied warranty of 14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15// GNU General Public License for more details. 16 17// You should have received a copy of the GNU General Public License along 18// with this library; see the file COPYING3. If not see 19// <http://www.gnu.org/licenses/>. 20 21// PR libstdc++/55463 Passing rvalue objects to std::mem_fn 22 23#include <functional> 24 25struct X 26{ 27 int& func(); 28 char& func_c() const; 29 short& func_v() volatile; 30 double& func_cv() const volatile; 31 32 int data; 33}; 34 35struct Y : X { }; 36 37using CX = const X; 38using CY = const Y; 39 40using X_ptr = X*; 41 42struct smart_ptr 43{ 44 X& operator*() const; 45}; 46 47std::reference_wrapper<X> ref(); 48std::reference_wrapper<const X> cref(); 49std::reference_wrapper<Y> yref(); 50 51void test01() 52{ 53 int& i1 __attribute__((unused)) = std::mem_fn( &X::func )( X() ); 54 int& i2 __attribute__((unused)) = std::mem_fn( &X::func )( Y() ); 55 int& i3 __attribute__((unused)) = std::mem_fn( &X::func )( ref() ); 56 int& i4 __attribute__((unused)) = std::mem_fn( &X::func )( yref() ); 57 int& i5 __attribute__((unused)) = std::mem_fn( &X::func )( X_ptr() ); 58 int& i6 __attribute__((unused)) = std::mem_fn( &X::func )( smart_ptr() ); 59 60 char& c1 __attribute__((unused)) = std::mem_fn( &X::func_c )( X() ); 61 char& c2 __attribute__((unused)) = std::mem_fn( &X::func_c )( CX() ); 62 char& c3 __attribute__((unused)) = std::mem_fn( &X::func_c )( Y() ); 63 char& c4 __attribute__((unused)) = std::mem_fn( &X::func_c )( ref() ); 64 char& c5 __attribute__((unused)) = std::mem_fn( &X::func_c )( cref() ); 65 char& c6 __attribute__((unused)) = std::mem_fn( &X::func_c )( yref() ); 66 char& c7 __attribute__((unused)) = std::mem_fn( &X::func_c )( X_ptr() ); 67 char& c8 __attribute__((unused)) = std::mem_fn( &X::func_c )( smart_ptr() ); 68 69 short& s1 __attribute__((unused)) = std::mem_fn( &X::func_v )( X() ); 70 short& s2 __attribute__((unused)) = std::mem_fn( &X::func_v )( Y() ); 71 short& s3 __attribute__((unused)) = std::mem_fn( &X::func_v )( ref() ); 72 short& s4 __attribute__((unused)) = std::mem_fn( &X::func_v )( yref() ); 73 short& s5 __attribute__((unused)) = std::mem_fn( &X::func_v )( X_ptr() ); 74 short& s6 __attribute__((unused)) = std::mem_fn( &X::func_v )( smart_ptr() ); 75 76 double& d1 __attribute__((unused)) = std::mem_fn( &X::func_cv )( X() ); 77 double& d2 __attribute__((unused)) = std::mem_fn( &X::func_cv )( CX() ); 78 double& d3 __attribute__((unused)) = std::mem_fn( &X::func_cv )( Y() ); 79 double& d4 __attribute__((unused)) = std::mem_fn( &X::func_cv )( ref() ); 80 double& d5 __attribute__((unused)) = std::mem_fn( &X::func_cv )( cref() ); 81 double& d6 __attribute__((unused)) = std::mem_fn( &X::func_cv )( yref() ); 82 double& d7 __attribute__((unused)) = std::mem_fn( &X::func_cv )( X_ptr() ); 83 double& d8 __attribute__((unused)) 84 = std::mem_fn( &X::func_cv )( smart_ptr() ); 85 86 // [expr.mptr.oper] 87 // The result of a .* expression whose second operand is a pointer to a 88 // data member is of the same value category (3.10) as its first operand. 89 int&& rval __attribute__((unused)) = std::mem_fn( &X::data )( X() ); 90 const int&& crval __attribute__((unused)) = std::mem_fn( &X::data )( CX() ); 91 int&& yrval __attribute__((unused)) = std::mem_fn( &X::data )( Y() ); 92 const int&& ycrval __attribute__((unused)) = std::mem_fn( &X::data )( CY() ); 93 94 int& val __attribute__((unused)) = std::mem_fn( &X::data )( ref() ); 95 const int& cval __attribute__((unused)) = std::mem_fn( &X::data )( cref() ); 96 int& yval __attribute__((unused)) = std::mem_fn( &X::data )( yref() ); 97 98 int& pval __attribute__((unused)) = std::mem_fn( &X::data )( X_ptr() ); 99 int& sval __attribute__((unused)) = std::mem_fn( &X::data )( smart_ptr() ); 100} 101 102void test02() 103{ 104 std::reference_wrapper<X> r = ref(); 105 X& x1 __attribute__((unused)) 106 = std::mem_fn( &std::reference_wrapper<X>::get )( r ); 107 const std::reference_wrapper<X> cr = ref(); 108 const X& x3 __attribute__((unused)) 109 = std::mem_fn( &std::reference_wrapper<X>::get )( cr ); 110 X& x2 __attribute__((unused)) 111 = std::mem_fn( &std::reference_wrapper<X>::get )( ref() ); 112} 113