]>
Commit | Line | Data |
---|---|---|
52066eae | 1 | // { dg-do run { target c++11 } } |
7cc9022f AA |
2 | // { dg-require-effective-target hosted } |
3 | ||
7aec2c63 PC |
4 | // 2005-01-15 Douglas Gregor <dgregor@cs.indiana.edu> |
5 | // | |
7adcbafe | 6 | // Copyright (C) 2005-2022 Free Software Foundation, Inc. |
7aec2c63 PC |
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 | ||
4704f28e JW |
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 | } | |
7aec2c63 | 41 | |
7aec2c63 PC |
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 ); | |
b6b66006 | 70 | #if __cpp_rtti |
4704f28e | 71 | VERIFY( f.target_type() == typeid(std::ref(x)) ); // LWG 2781 |
b6b66006 | 72 | #endif |
4704f28e | 73 | VERIFY( wraps(f, x) ); |
7aec2c63 PC |
74 | |
75 | function<int()> g = f; | |
76 | VERIFY( g ); | |
77 | VERIFY( g() == 17 ); | |
b6b66006 | 78 | #if __cpp_rtti |
4704f28e | 79 | VERIFY( g.target_type() == f.target_type() ); |
b6b66006 | 80 | #endif |
4704f28e | 81 | VERIFY( wraps(g, x) ); |
7aec2c63 PC |
82 | |
83 | function<int()> h = cref(x); | |
84 | VERIFY( h ); | |
85 | VERIFY( h() == 42 ); | |
b6b66006 | 86 | #if __cpp_rtti |
4704f28e | 87 | VERIFY( h.target_type() == typeid(std::cref(x)) ); |
b6b66006 | 88 | #endif |
4704f28e | 89 | VERIFY( wraps(h, as_const(x)) ); |
7aec2c63 PC |
90 | |
91 | const function<int()>& hc = h; | |
b6b66006 | 92 | #if __cpp_rtti |
4704f28e | 93 | VERIFY( hc.target_type() == h.target_type() ); |
b6b66006 | 94 | #endif |
4704f28e | 95 | VERIFY( wraps(hc, as_const(x)) ); |
7aec2c63 PC |
96 | } |
97 | ||
98 | int main() | |
99 | { | |
100 | test06(); | |
101 | return 0; | |
102 | } |