| 1 | // { dg-do run { target c++11 } } |
| 2 | // { dg-require-effective-target hosted } |
| 3 | |
| 4 | // 2005-01-15 Douglas Gregor <[email protected]> |
| 5 | // |
| 6 | // Copyright (C) 2005-2025 Free Software Foundation, Inc. |
| 7 | // |
| 8 | // This file is part of the GNU ISO C++ Library. This library is free |
| 9 | // software; you can redistribute it and/or modify it under the |
| 10 | // terms of the GNU General Public License as published by the |
| 11 | // Free Software Foundation; either version 3, or (at your option) |
| 12 | // any later version. |
| 13 | // |
| 14 | // This library is distributed in the hope that it will be useful, |
| 15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | // GNU General Public License for more details. |
| 18 | // |
| 19 | // You should have received a copy of the GNU General Public License along |
| 20 | // with this library; see the file COPYING3. If not see |
| 21 | // <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | // 20.7.15 polymorphic function object wrapper |
| 24 | #include <functional> |
| 25 | #include <testsuite_hooks.h> |
| 26 | #include <testsuite_tr1.h> |
| 27 | |
| 28 | template<typename T> |
| 29 | const T& |
| 30 | as_const(T& t) |
| 31 | { return t; } |
| 32 | |
| 33 | // Check that f's target is a reference_wrapper bound to obj. |
| 34 | template<typename Function, typename T> |
| 35 | bool |
| 36 | wraps(Function& f, T& obj) |
| 37 | { |
| 38 | using ref_wrapper_type = std::reference_wrapper<T>; |
| 39 | auto* p = f.template target<ref_wrapper_type>(); |
| 40 | return std::addressof(p->get()) == std::addressof(obj); |
| 41 | } |
| 42 | |
| 43 | // Put reference_wrappers to function pointers into function<> wrappers |
| 44 | void test07() |
| 45 | { |
| 46 | using std::function; |
| 47 | using std::ref; |
| 48 | using std::cref; |
| 49 | using std::reference_wrapper; |
| 50 | |
| 51 | int (*fptr)(float) = __gnu_test::truncate_float; |
| 52 | |
| 53 | function<int(float)> f1(ref(fptr)); |
| 54 | VERIFY( f1 ); |
| 55 | VERIFY( !!f1 ); |
| 56 | VERIFY( !(f1 == 0) ); |
| 57 | VERIFY( !(0 == f1) ); |
| 58 | VERIFY( f1 != 0 ); |
| 59 | VERIFY( 0 != f1 ); |
| 60 | |
| 61 | // Invocation |
| 62 | VERIFY( f1(3.1f) == 3 ); |
| 63 | |
| 64 | // target_type and target() functions |
| 65 | const function<int(float)>& f1c = f1; |
| 66 | using ref_wrapper_type = reference_wrapper<int(*)(float)>; |
| 67 | #if __cpp_rtti |
| 68 | VERIFY( typeid(ref_wrapper_type) == f1.target_type() ); |
| 69 | #endif |
| 70 | VERIFY( f1.target<ref_wrapper_type>() != nullptr ); |
| 71 | VERIFY( wraps(f1, fptr) ); |
| 72 | VERIFY( wraps(f1c, fptr) ); |
| 73 | |
| 74 | function<int(float)> f2(cref(fptr)); |
| 75 | VERIFY( f2 ); |
| 76 | VERIFY( !!f2 ); |
| 77 | VERIFY( !(f2 == 0) ); |
| 78 | VERIFY( !(0 == f2) ); |
| 79 | VERIFY( f2 != 0 ); |
| 80 | VERIFY( 0 != f2 ); |
| 81 | |
| 82 | // Invocation |
| 83 | VERIFY( f2(3.1f) == 3 ); |
| 84 | |
| 85 | // target_type and target() functions |
| 86 | const function<int(float)>& f2c = f2; |
| 87 | using cref_wrapper_type = reference_wrapper<int(* const)(float)>; |
| 88 | #if __cpp_rtti |
| 89 | VERIFY( typeid(cref_wrapper_type) == f2.target_type() ); |
| 90 | #endif |
| 91 | VERIFY( wraps(f2, as_const(fptr)) ); |
| 92 | #if __cpp_rtti |
| 93 | VERIFY( f2c.target_type() == f2.target_type() ); |
| 94 | #endif |
| 95 | VERIFY( wraps(f2c, as_const(fptr)) ); |
| 96 | } |
| 97 | |
| 98 | int main() |
| 99 | { |
| 100 | test07(); |
| 101 | return 0; |
| 102 | } |