]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/tr1/3_function_objects/reference_wrapper/invoke.cc
Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / tr1 / 3_function_objects / reference_wrapper / invoke.cc
CommitLineData
3c235000
DG
1// 2005-02-27 Douglas Gregor <doug.gregor -at- gmail.com>
2//
748086b7 3// Copyright (C) 2005, 2009 Free Software Foundation, Inc.
3c235000
DG
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
748086b7 8// Free Software Foundation; either version 3, or (at your option)
3c235000
DG
9// any later version.
10//
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License along
748086b7
JJ
17// with this library; see the file COPYING3. If not see
18// <http://www.gnu.org/licenses/>.
3c235000
DG
19
20// 2.1 reference wrappers
21#include <tr1/functional>
22#include <tr1/type_traits>
23#include <testsuite_hooks.h>
24#include <testsuite_tr1.h>
25
26using namespace __gnu_test;
27
28bool test __attribute__((unused)) = true;
29
30struct X
31{
32 typedef int result_type;
33
34 X() : bar(17) {}
35
36 int foo(float x) { return truncate_float(x); }
37 int foo_c(float x) const { return truncate_float(x); }
38 int foo_v(float x) volatile { return truncate_float(x); }
39 int foo_cv(float x) const volatile { return truncate_float(x); }
40
41 int operator()(float x)
42 {
43 return foo(x) + 1;
44 }
45
46 int operator()(float x) const
47 {
48 return foo_c(x) + 2;
49 }
50
51 int bar;
52
53 private:
54 X(const X&);
55 X& operator=(const X&);
56};
57
3270a66b
PC
58int seventeen() { return 17; }
59
60struct get_seventeen
61{
62 typedef int result_type;
63 int operator()() const { return 17; }
64};
65
3c235000
DG
66void test01()
67{
68 using std::tr1::ref;
69 using std::tr1::cref;
70
3270a66b 71 ::get_seventeen get_sev;
3c235000
DG
72 ::X x;
73 ::X* xp = &x;
74 int (::X::* p_foo)(float) = &::X::foo;
75 int (::X::* p_foo_c)(float) const = &::X::foo_c;
76 int (::X::* p_foo_v)(float) volatile = &::X::foo_v;
77 int (::X::* p_foo_cv)(float) const volatile = &::X::foo_cv;
78 int ::X::* p_bar = &::X::bar;
79
80 const float pi = 3.14;
81
82 // Functions
83 VERIFY(ref(truncate_float)(pi) == 3);
3270a66b 84 VERIFY(ref(seventeen)() == 17);
3c235000
DG
85
86 // Function pointers
87 VERIFY(cref(&truncate_float)(pi) == 3);
3270a66b 88 VERIFY(cref(&seventeen)() == 17);
3c235000
DG
89
90 // Member function pointers
91 VERIFY(ref(p_foo)(x, pi) == 3);
92 VERIFY(ref(p_foo)(xp, pi) == 3);
93 VERIFY(ref(p_foo_c)(x, pi) == 3);
94 VERIFY(ref(p_foo_c)(xp, pi) == 3);
95 VERIFY(ref(p_foo_v)(x, pi) == 3);
96 VERIFY(ref(p_foo_v)(xp, pi) == 3);
97 VERIFY(ref(p_foo_cv)(x, pi) == 3);
98 VERIFY(ref(p_foo_cv)(xp, pi) == 3);
99
100 // Member data pointers
101 VERIFY(ref(p_bar)(x) == 17);
102 VERIFY(ref(p_bar)(xp) == 17);
103
104 // Function objects
3270a66b
PC
105 VERIFY(ref(get_sev)() == 17);
106 VERIFY(cref(get_sev)() == 17);
3c235000
DG
107 VERIFY(ref(x)(pi) == 4);
108 VERIFY(cref(x)(pi) == 5);
109}
110
111int main()
112{
113 test01();
114 return 0;
115}