]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/function/7.cc
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>
24 #include <testsuite_tr1.h>
31 // Check that f's target is a reference_wrapper bound to obj.
32 template<typename Function
, typename T
>
34 wraps(Function
& f
, T
& obj
)
36 using ref_wrapper_type
= std::reference_wrapper
<T
>;
37 auto* p
= f
.template target
<ref_wrapper_type
>();
38 return std::addressof(p
->get()) == std::addressof(obj
);
41 // Put reference_wrappers to function pointers into function<> wrappers
47 using std::reference_wrapper
;
49 int (*fptr
)(float) = __gnu_test::truncate_float
;
51 function
<int(float)> f1(ref(fptr
));
60 VERIFY( f1(3.1f
) == 3 );
62 // target_type and target() functions
63 const function
<int(float)>& f1c
= f1
;
64 using ref_wrapper_type
= reference_wrapper
<int(*)(float)>;
65 VERIFY( typeid(ref_wrapper_type
) == f1
.target_type() );
66 VERIFY( f1
.target
<ref_wrapper_type
>() != nullptr );
67 VERIFY( wraps(f1
, fptr
) );
68 VERIFY( wraps(f1c
, fptr
) );
70 function
<int(float)> f2(cref(fptr
));
79 VERIFY( f2(3.1f
) == 3 );
81 // target_type and target() functions
82 const function
<int(float)>& f2c
= f2
;
83 using cref_wrapper_type
= reference_wrapper
<int(* const)(float)>;
84 VERIFY( typeid(cref_wrapper_type
) == f2
.target_type() );
85 VERIFY( wraps(f2
, as_const(fptr
)) );
86 VERIFY( f2c
.target_type() == f2
.target_type() );
87 VERIFY( wraps(f2c
, as_const(fptr
)) );