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