]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/function/7.cc
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-2024 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>
26 #include <testsuite_tr1.h>
33 // Check that f's target is a reference_wrapper bound to obj.
34 template<typename Function
, typename T
>
36 wraps(Function
& f
, T
& obj
)
38 using ref_wrapper_type
= std::reference_wrapper
<T
>;
39 auto* p
= f
.template target
<ref_wrapper_type
>();
40 return std::addressof(p
->get()) == std::addressof(obj
);
43 // Put reference_wrappers to function pointers into function<> wrappers
49 using std::reference_wrapper
;
51 int (*fptr
)(float) = __gnu_test::truncate_float
;
53 function
<int(float)> f1(ref(fptr
));
62 VERIFY( f1(3.1f
) == 3 );
64 // target_type and target() functions
65 const function
<int(float)>& f1c
= f1
;
66 using ref_wrapper_type
= reference_wrapper
<int(*)(float)>;
68 VERIFY( typeid(ref_wrapper_type
) == f1
.target_type() );
70 VERIFY( f1
.target
<ref_wrapper_type
>() != nullptr );
71 VERIFY( wraps(f1
, fptr
) );
72 VERIFY( wraps(f1c
, fptr
) );
74 function
<int(float)> f2(cref(fptr
));
83 VERIFY( f2(3.1f
) == 3 );
85 // target_type and target() functions
86 const function
<int(float)>& f2c
= f2
;
87 using cref_wrapper_type
= reference_wrapper
<int(* const)(float)>;
89 VERIFY( typeid(cref_wrapper_type
) == f2
.target_type() );
91 VERIFY( wraps(f2
, as_const(fptr
)) );
93 VERIFY( f2c
.target_type() == f2
.target_type() );
95 VERIFY( wraps(f2c
, as_const(fptr
)) );