]>
Commit | Line | Data |
---|---|---|
52066eae | 1 | // { dg-do run { target c++11 } } |
7aec2c63 PC |
2 | // 2005-01-15 Douglas Gregor <dgregor@cs.indiana.edu> |
3 | // | |
99dee823 | 4 | // Copyright (C) 2005-2021 Free Software Foundation, Inc. |
7aec2c63 PC |
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 | // 20.7.15 polymorphic function object wrapper | |
22 | #include <functional> | |
23 | #include <testsuite_hooks.h> | |
24 | ||
4704f28e JW |
25 | template<typename T> |
26 | const T& | |
27 | as_const(T& t) | |
28 | { return t; } | |
29 | ||
30 | // Check that f's target is a reference_wrapper bound to obj. | |
31 | template<typename Function, typename T> | |
32 | bool | |
33 | wraps(Function& f, T& obj) | |
34 | { | |
35 | using ref_wrapper_type = std::reference_wrapper<T>; | |
36 | auto* p = f.template target<ref_wrapper_type>(); | |
37 | return std::addressof(p->get()) == std::addressof(obj); | |
38 | } | |
7aec2c63 | 39 | |
7aec2c63 PC |
40 | struct secret {}; |
41 | ||
42 | struct noncopyable_function_object_type | |
43 | { | |
44 | noncopyable_function_object_type(secret) {} | |
45 | ||
46 | int operator()() const { return 42; } | |
47 | int operator()() { return 17; } | |
48 | ||
49 | private: | |
50 | noncopyable_function_object_type(); | |
51 | noncopyable_function_object_type(const noncopyable_function_object_type&); | |
52 | void operator=(const noncopyable_function_object_type&); | |
53 | }; | |
54 | ||
55 | // Put reference_wrappers into function<> wrappers | |
56 | void test06() | |
57 | { | |
58 | using std::function; | |
59 | using std::ref; | |
60 | using std::cref; | |
61 | ||
62 | secret password; | |
63 | noncopyable_function_object_type x(password); | |
64 | ||
65 | function<int()> f(ref(x)); | |
66 | VERIFY( f ); | |
67 | VERIFY( f() == 17 ); | |
4704f28e JW |
68 | VERIFY( f.target_type() == typeid(std::ref(x)) ); // LWG 2781 |
69 | VERIFY( wraps(f, x) ); | |
7aec2c63 PC |
70 | |
71 | function<int()> g = f; | |
72 | VERIFY( g ); | |
73 | VERIFY( g() == 17 ); | |
4704f28e JW |
74 | VERIFY( g.target_type() == f.target_type() ); |
75 | VERIFY( wraps(g, x) ); | |
7aec2c63 PC |
76 | |
77 | function<int()> h = cref(x); | |
78 | VERIFY( h ); | |
79 | VERIFY( h() == 42 ); | |
4704f28e JW |
80 | VERIFY( h.target_type() == typeid(std::cref(x)) ); |
81 | VERIFY( wraps(h, as_const(x)) ); | |
7aec2c63 PC |
82 | |
83 | const function<int()>& hc = h; | |
4704f28e JW |
84 | VERIFY( hc.target_type() == h.target_type() ); |
85 | VERIFY( wraps(hc, as_const(x)) ); | |
7aec2c63 PC |
86 | } |
87 | ||
88 | int main() | |
89 | { | |
90 | test06(); | |
91 | return 0; | |
92 | } |