1 // { dg-do run { target c++11 } }
2 // 2005-01-15 Douglas Gregor <dgregor@cs.indiana.edu>
4 // Copyright (C) 2005-2021 Free Software Foundation, Inc.
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)
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.
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/>.
21 // 20.7.15 polymorphic function object wrapper
23 #include <testsuite_hooks.h>
30 // Check that f's target is a reference_wrapper bound to obj.
31 template<typename Function
, typename T
>
33 wraps(Function
& f
, T
& obj
)
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
);
42 struct noncopyable_function_object_type
44 noncopyable_function_object_type(secret
) {}
46 int operator()() const { return 42; }
47 int operator()() { return 17; }
50 noncopyable_function_object_type();
51 noncopyable_function_object_type(const noncopyable_function_object_type
&);
52 void operator=(const noncopyable_function_object_type
&);
55 // Put reference_wrappers into function<> wrappers
63 noncopyable_function_object_type
x(password
);
65 function
<int()> f(ref(x
));
68 VERIFY( f
.target_type() == typeid(std::ref(x
)) ); // LWG 2781
69 VERIFY( wraps(f
, x
) );
71 function
<int()> g
= f
;
74 VERIFY( g
.target_type() == f
.target_type() );
75 VERIFY( wraps(g
, x
) );
77 function
<int()> h
= cref(x
);
80 VERIFY( h
.target_type() == typeid(std::cref(x
)) );
81 VERIFY( wraps(h
, as_const(x
)) );
83 const function
<int()>& hc
= h
;
84 VERIFY( hc
.target_type() == h
.target_type() );
85 VERIFY( wraps(hc
, as_const(x
)) );