1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <functional>
11
12
13 // In C++17, the function adapters mem_fun/mem_fun_ref, etc have been removed.
14 // However, for backwards compatibility, if _LIBCPP_ENABLE_CXX17_REMOVED_BINDERS
15 // is defined before including <functional>, then they will be restored.
16
17 // MODULES_DEFINES: _LIBCPP_ENABLE_CXX17_REMOVED_BINDERS
18 #define _LIBCPP_ENABLE_CXX17_REMOVED_BINDERS
19
20 #include <functional>
21 #include <cassert>
22
identity(int v)23 int identity(int v) { return v; }
sum(int a,int b)24 int sum(int a, int b) { return a + b; }
25
26 struct Foo {
zeroFoo27 int zero() { return 0; }
zero_constFoo28 int zero_const() const { return 1; }
29
identityFoo30 int identity(int v) const { return v; }
sumFoo31 int sum(int a, int b) const { return a + b; }
32 };
33
main()34 int main()
35 {
36 typedef std::pointer_to_unary_function<int, int> PUF;
37 typedef std::pointer_to_binary_function<int, int, int> PBF;
38
39 static_assert(
40 (std::is_same<PUF, decltype((std::ptr_fun<int, int>(identity)))>::value),
41 "");
42 static_assert(
43 (std::is_same<PBF, decltype((std::ptr_fun<int, int, int>(sum)))>::value),
44 "");
45
46 assert((std::ptr_fun<int, int>(identity)(4) == 4));
47 assert((std::ptr_fun<int, int, int>(sum)(4, 5) == 9));
48
49 Foo f;
50 assert((std::mem_fn(&Foo::identity)(f, 5) == 5));
51 assert((std::mem_fn(&Foo::sum)(f, 5, 6) == 11));
52
53 typedef std::mem_fun_ref_t<int, Foo> MFR;
54 typedef std::const_mem_fun_ref_t<int, Foo> CMFR;
55
56 static_assert(
57 (std::is_same<MFR, decltype((std::mem_fun_ref(&Foo::zero)))>::value), "");
58 static_assert((std::is_same<CMFR, decltype((std::mem_fun_ref(
59 &Foo::zero_const)))>::value),
60 "");
61
62 assert((std::mem_fun_ref(&Foo::zero)(f) == 0));
63 assert((std::mem_fun_ref(&Foo::identity)(f, 5) == 5));
64 }
65