| 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 | |
| 27 | template<typename T> |
| 28 | const T& |
| 29 | as_const(T& t) |
| 30 | { return t; } |
| 31 | |
| 32 | // Check that f's target is a reference_wrapper bound to obj. |
| 33 | template<typename Function, typename T> |
| 34 | bool |
| 35 | wraps(Function& f, T& obj) |
| 36 | { |
| 37 | using ref_wrapper_type = std::reference_wrapper<T>; |
| 38 | auto* p = f.template target<ref_wrapper_type>(); |
| 39 | return std::addressof(p->get()) == std::addressof(obj); |
| 40 | } |
| 41 | |
| 42 | struct secret {}; |
| 43 | |
| 44 | struct noncopyable_function_object_type |
| 45 | { |
| 46 | noncopyable_function_object_type(secret) {} |
| 47 | |
| 48 | int operator()() const { return 42; } |
| 49 | int operator()() { return 17; } |
| 50 | |
| 51 | private: |
| 52 | noncopyable_function_object_type(); |
| 53 | noncopyable_function_object_type(const noncopyable_function_object_type&); |
| 54 | void operator=(const noncopyable_function_object_type&); |
| 55 | }; |
| 56 | |
| 57 | // Put reference_wrappers into function<> wrappers |
| 58 | void test06() |
| 59 | { |
| 60 | using std::function; |
| 61 | using std::ref; |
| 62 | using std::cref; |
| 63 | |
| 64 | secret password; |
| 65 | noncopyable_function_object_type x(password); |
| 66 | |
| 67 | function<int()> f(ref(x)); |
| 68 | VERIFY( f ); |
| 69 | VERIFY( f() == 17 ); |
| 70 | #if __cpp_rtti |
| 71 | VERIFY( f.target_type() == typeid(std::ref(x)) ); // LWG 2781 |
| 72 | #endif |
| 73 | VERIFY( wraps(f, x) ); |
| 74 | |
| 75 | function<int()> g = f; |
| 76 | VERIFY( g ); |
| 77 | VERIFY( g() == 17 ); |
| 78 | #if __cpp_rtti |
| 79 | VERIFY( g.target_type() == f.target_type() ); |
| 80 | #endif |
| 81 | VERIFY( wraps(g, x) ); |
| 82 | |
| 83 | function<int()> h = cref(x); |
| 84 | VERIFY( h ); |
| 85 | VERIFY( h() == 42 ); |
| 86 | #if __cpp_rtti |
| 87 | VERIFY( h.target_type() == typeid(std::cref(x)) ); |
| 88 | #endif |
| 89 | VERIFY( wraps(h, as_const(x)) ); |
| 90 | |
| 91 | const function<int()>& hc = h; |
| 92 | #if __cpp_rtti |
| 93 | VERIFY( hc.target_type() == h.target_type() ); |
| 94 | #endif |
| 95 | VERIFY( wraps(hc, as_const(x)) ); |
| 96 | } |
| 97 | |
| 98 | int main() |
| 99 | { |
| 100 | test06(); |
| 101 | return 0; |
| 102 | } |