1 // { dg-do run { target c++11 } }
2 // { dg-require-effective-target hosted }
4 // 2005-01-15 Douglas Gregor <dgregor@cs.indiana.edu>
6 // Copyright (C) 2005-2023 Free Software Foundation, Inc.
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)
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.
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/>.
23 // 20.7.15 polymorphic function object wrapper
25 #include <testsuite_hooks.h>
32 // Check that f's target is a reference_wrapper bound to obj.
33 template<typename Function
, typename T
>
35 wraps(Function
& f
, T
& obj
)
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
);
44 struct noncopyable_function_object_type
46 noncopyable_function_object_type(secret
) {}
48 int operator()() const { return 42; }
49 int operator()() { return 17; }
52 noncopyable_function_object_type();
53 noncopyable_function_object_type(const noncopyable_function_object_type
&);
54 void operator=(const noncopyable_function_object_type
&);
57 // Put reference_wrappers into function<> wrappers
65 noncopyable_function_object_type
x(password
);
67 function
<int()> f(ref(x
));
71 VERIFY( f
.target_type() == typeid(std::ref(x
)) ); // LWG 2781
73 VERIFY( wraps(f
, x
) );
75 function
<int()> g
= f
;
79 VERIFY( g
.target_type() == f
.target_type() );
81 VERIFY( wraps(g
, x
) );
83 function
<int()> h
= cref(x
);
87 VERIFY( h
.target_type() == typeid(std::cref(x
)) );
89 VERIFY( wraps(h
, as_const(x
)) );
91 const function
<int()>& hc
= h
;
93 VERIFY( hc
.target_type() == h
.target_type() );
95 VERIFY( wraps(hc
, as_const(x
)) );