1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // <functional>
10
11 // template<Returnable R, class T, CopyConstructible... Args>
12 // unspecified mem_fn(R (T::* pm)(Args...) const);
13
14 #include <functional>
15 #include <cassert>
16
17 #include "test_macros.h"
18
19 struct A
20 {
test0A21 TEST_CONSTEXPR char test0() const {return 'a';}
test1A22 TEST_CONSTEXPR char test1(int) const {return 'b';}
test2A23 TEST_CONSTEXPR char test2(int, double) const {return 'c';}
24 };
25
26 template <class F>
27 TEST_CONSTEXPR_CXX20 bool
test0(F f)28 test0(F f)
29 {
30 {
31 A a;
32 assert(f(a) == 'a');
33 A* ap = &a;
34 assert(f(ap) == 'a');
35 const A* cap = &a;
36 assert(f(cap) == 'a');
37 const F& cf = f;
38 assert(cf(ap) == 'a');
39 }
40 return true;
41 }
42
43 template <class F>
44 TEST_CONSTEXPR_CXX20 bool
test1(F f)45 test1(F f)
46 {
47 {
48 A a;
49 assert(f(a, 1) == 'b');
50 A* ap = &a;
51 assert(f(ap, 2) == 'b');
52 const A* cap = &a;
53 assert(f(cap, 2) == 'b');
54 const F& cf = f;
55 assert(cf(ap, 2) == 'b');
56 }
57 return true;
58 }
59
60 template <class F>
61 TEST_CONSTEXPR_CXX20 bool
test2(F f)62 test2(F f)
63 {
64 {
65 A a;
66 assert(f(a, 1, 2) == 'c');
67 A* ap = &a;
68 assert(f(ap, 2, 3.5) == 'c');
69 const A* cap = &a;
70 assert(f(cap, 2, 3.5) == 'c');
71 const F& cf = f;
72 assert(cf(ap, 2, 3.5) == 'c');
73 }
74 return true;
75 }
76
main(int,char **)77 int main(int, char**)
78 {
79 test0(std::mem_fn(&A::test0));
80 test1(std::mem_fn(&A::test1));
81 test2(std::mem_fn(&A::test2));
82
83 #if TEST_STD_VER >= 20
84 static_assert(test0(std::mem_fn(&A::test0)));
85 static_assert(test1(std::mem_fn(&A::test1)));
86 static_assert(test2(std::mem_fn(&A::test2)));
87 #endif
88
89 return 0;
90 }
91