Update copyright in libstdc++-v3.
[official-gcc.git] / libstdc++-v3 / testsuite / 20_util / function / 8.cc
blob5f471ce847029b42d2366fe9b52d6d4191cd6c9b
1 // { dg-options "-std=gnu++0x" }
2 // 2005-01-15 Douglas Gregor <dgregor@cs.indiana.edu>
3 //
4 // Copyright (C) 2005-2013 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.
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
22 #include <functional>
23 #include <testsuite_hooks.h>
24 #include <testsuite_tr1.h>
26 using namespace __gnu_test;
28 bool test __attribute__((unused)) = true;
30 // Put reference_wrappers to member pointers
31 void test08()
33 using std::function;
34 using std::ref;
35 using std::cref;
37 int X::* X_bar = &X::bar;
38 int (X::* X_foo)() = &X::foo;
39 int (X::* X_foo_c)() const = &X::foo_c;
40 int (X::* X_foo_v)() volatile = &X::foo_v;
41 int (X::* X_foo_cv)() const volatile = &X::foo_cv;
43 X x;
44 x.bar = 17;
46 function<int(X&)> frm(ref(X_bar));
47 VERIFY( frm );
48 VERIFY( frm(x) == 17 );
49 VERIFY( typeid(int X::*) == frm.target_type() );
50 VERIFY( frm.target<int X::*>() == &X_bar );
52 function<int(X&)> fr(ref(X_foo));
53 VERIFY( fr );
54 VERIFY( fr(x) == 1 );
55 VERIFY( typeid(int (X::*)()) == fr.target_type() );
56 VERIFY( fr.target<int (X::*)()>() == &X_foo );
58 function<int(const X&)> frc(ref(X_foo_c));
59 VERIFY( frc );
60 VERIFY( frc(x) == 2 );
61 VERIFY( typeid(int (X::*)() const) == frc.target_type() );
62 VERIFY( frc.target<int (X::*)() const >() == &X_foo_c );
64 function<int(volatile X&)> frv(ref(X_foo_v));
65 VERIFY( frv );
66 VERIFY( frv(x) == 3 );
67 VERIFY( typeid(int (X::*)() volatile) == frv.target_type() );
68 VERIFY( *frv.target<int (X::*)() volatile >() == X_foo_v );
69 VERIFY( frv.target<int (X::*)() const volatile>() == 0 );
71 function<int(const volatile X&)> frcv(ref(X_foo_cv));
72 VERIFY( frcv );
73 VERIFY( frcv(x) == 4 );
74 VERIFY( typeid(int (X::*)() const volatile) == frcv.target_type() );
75 VERIFY( *frcv.target<int (X::*)() const volatile >() == X_foo_cv );
76 VERIFY( frcv.target<int (X::*)() const>() == 0 );
78 function<int(X*)> grm(ref(X_bar));
79 VERIFY( grm );
80 VERIFY( grm(&x) == 17 );
81 VERIFY( typeid(int X::*) == grm.target_type() );
82 VERIFY( *grm.target<int X::*>() == X_bar );
84 function<int(X*)> gr(ref(X_foo));
85 VERIFY( gr );
86 VERIFY( gr(&x) == 1 );
87 VERIFY( typeid(int (X::*)()) == gr.target_type() );
88 VERIFY( *gr.target<int (X::*)()>() == X_foo );
90 function<int(const X*)> grc(ref(X_foo_c));
91 VERIFY( grc );
92 VERIFY( grc(&x) == 2 );
93 VERIFY( typeid(int (X::*)() const) == grc.target_type() );
94 VERIFY( *grc.target<int (X::*)() const >() == X_foo_c );
96 function<int(volatile X*)> grv(ref(X_foo_v));
97 VERIFY( grv );
98 VERIFY( grv(&x) == 3 );
99 VERIFY( typeid(int (X::*)() volatile) == grv.target_type() );
100 VERIFY( *grv.target<int (X::*)() volatile >() == X_foo_v );
101 VERIFY( grv.target<int (X::*)() const volatile>() == 0 );
103 function<int(const volatile X*)> grcv(ref(X_foo_cv));
104 VERIFY( grcv );
105 VERIFY( grcv(&x) == 4 );
106 VERIFY( typeid(int (X::*)() const volatile) == grcv.target_type() );
107 VERIFY( *grcv.target<int (X::*)() const volatile >() == X_foo_cv );
108 VERIFY( grcv.target<int (X::*)() const>() == 0 );
110 function<int(X&)> hrm(cref(X_bar));
111 VERIFY( hrm );
112 VERIFY( hrm(x) == 17 );
113 VERIFY( typeid(int X::*) == hrm.target_type() );
114 VERIFY( hrm.target<int X::*>() == 0 );
115 VERIFY( hrm.target<int X::* const>() == &X_bar );
117 function<int(X&)> hr(cref(X_foo));
118 VERIFY( hr );
119 VERIFY( hr(x) == 1 );
120 VERIFY( typeid(int (X::*)()) == hr.target_type() );
121 VERIFY( hr.target<int (X::* const)()>() == &X_foo );
123 function<int(const X&)> hrc(cref(X_foo_c));
124 VERIFY( hrc );
125 VERIFY( hrc(x) == 2 );
126 VERIFY( typeid(int (X::*)() const) == hrc.target_type() );
127 VERIFY( hrc.target<int (X::* const)() const >() == &X_foo_c );
129 function<int(volatile X&)> hrv(cref(X_foo_v));
130 VERIFY( hrv );
131 VERIFY( hrv(x) == 3 );
132 VERIFY( typeid(int (X::*)() volatile) == hrv.target_type() );
133 VERIFY( hrv.target<int (X::* const)() volatile >() == &X_foo_v );
134 VERIFY( hrv.target<int (X::* const)() const volatile>() == 0 );
136 function<int(const volatile X&)> hrcv(cref(X_foo_cv));
137 VERIFY( hrcv );
138 VERIFY( hrcv(x) == 4 );
139 VERIFY( typeid(int (X::*)() const volatile) == hrcv.target_type() );
140 VERIFY( hrcv.target<int (X::* const)() const volatile >() == &X_foo_cv );
141 VERIFY( hrcv.target<int (X::* const)() const>() == 0 );
144 int main()
146 test08();
147 return 0;