]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/function/7.cc
Update copyright years.
[thirdparty/gcc.git] / 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>
3 //
4 // Copyright (C) 2005-2021 Free Software Foundation, Inc.
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 #include <testsuite_tr1.h>
25
26 template<typename T>
27 const T&
28 as_const(T& t)
29 { return t; }
30
31 // Check that f's target is a reference_wrapper bound to obj.
32 template<typename Function, typename T>
33 bool
34 wraps(Function& f, T& obj)
35 {
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);
39 }
40
41 // Put reference_wrappers to function pointers into function<> wrappers
42 void test07()
43 {
44 using std::function;
45 using std::ref;
46 using std::cref;
47 using std::reference_wrapper;
48
49 int (*fptr)(float) = __gnu_test::truncate_float;
50
51 function<int(float)> f1(ref(fptr));
52 VERIFY( f1 );
53 VERIFY( !!f1 );
54 VERIFY( !(f1 == 0) );
55 VERIFY( !(0 == f1) );
56 VERIFY( f1 != 0 );
57 VERIFY( 0 != f1 );
58
59 // Invocation
60 VERIFY( f1(3.1f) == 3 );
61
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) );
69
70 function<int(float)> f2(cref(fptr));
71 VERIFY( f2 );
72 VERIFY( !!f2 );
73 VERIFY( !(f2 == 0) );
74 VERIFY( !(0 == f2) );
75 VERIFY( f2 != 0 );
76 VERIFY( 0 != f2 );
77
78 // Invocation
79 VERIFY( f2(3.1f) == 3 );
80
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)) );
88 }
89
90 int main()
91 {
92 test07();
93 return 0;
94 }