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...)); 13 14 #include <functional> 15 #include <cassert> 16 17 #include "test_macros.h" 18 19 struct A 20 { 21 TEST_CONSTEXPR_CXX14 char test0() {return 'a';} 22 TEST_CONSTEXPR_CXX14 char test1(int) {return 'b';} 23 TEST_CONSTEXPR_CXX14 char test2(int, double) {return 'c';} 24 }; 25 26 template <class F> 27 TEST_CONSTEXPR_CXX20 bool 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 F& cf = f; 36 assert(cf(ap) == 'a'); 37 } 38 return true; 39 } 40 41 template <class F> 42 TEST_CONSTEXPR_CXX20 bool 43 test1(F f) 44 { 45 { 46 A a; 47 assert(f(a, 1) == 'b'); 48 A* ap = &a; 49 assert(f(ap, 2) == 'b'); 50 const F& cf = f; 51 assert(cf(ap, 2) == 'b'); 52 } 53 return true; 54 } 55 56 template <class F> 57 TEST_CONSTEXPR_CXX20 bool 58 test2(F f) 59 { 60 { 61 A a; 62 assert(f(a, 1, 2) == 'c'); 63 A* ap = &a; 64 assert(f(ap, 2, 3.5) == 'c'); 65 const F& cf = f; 66 assert(cf(ap, 2, 3.5) == 'c'); 67 } 68 return true; 69 } 70 71 int main(int, char**) 72 { 73 test0(std::mem_fn(&A::test0)); 74 test1(std::mem_fn(&A::test1)); 75 test2(std::mem_fn(&A::test2)); 76 #if TEST_STD_VER >= 11 77 static_assert((noexcept(std::mem_fn(&A::test0))), ""); // LWG#2489 78 #endif 79 80 #if TEST_STD_VER >= 20 81 static_assert(test0(std::mem_fn(&A::test0))); 82 static_assert(test1(std::mem_fn(&A::test1))); 83 static_assert(test2(std::mem_fn(&A::test2))); 84 #endif 85 86 return 0; 87 } 88