Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <functional> |
| 11 | |
| 12 | // template<Returnable R, class T, CopyConstructible... Args> |
| 13 | // unspecified mem_fn(R (T::* pm)(Args...)); |
| 14 | |
| 15 | #include <functional> |
| 16 | #include <cassert> |
| 17 | |
| 18 | struct A |
| 19 | { |
| 20 | char test0() {return 'a';} |
| 21 | char test1(int) {return 'b';} |
| 22 | char test2(int, double) {return 'c';} |
| 23 | }; |
| 24 | |
| 25 | template <class F> |
| 26 | void |
| 27 | test0(F f) |
| 28 | { |
| 29 | { |
| 30 | A a; |
| 31 | assert(f(a) == 'a'); |
| 32 | A* ap = &a; |
| 33 | assert(f(ap) == 'a'); |
Peter Collingbourne | a4c0d87 | 2014-01-22 22:56:52 +0000 | [diff] [blame] | 34 | const F& cf = f; |
| 35 | assert(cf(ap) == 'a'); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
| 39 | template <class F> |
| 40 | void |
| 41 | test1(F f) |
| 42 | { |
| 43 | { |
| 44 | A a; |
| 45 | assert(f(a, 1) == 'b'); |
| 46 | A* ap = &a; |
| 47 | assert(f(ap, 2) == 'b'); |
Peter Collingbourne | a4c0d87 | 2014-01-22 22:56:52 +0000 | [diff] [blame] | 48 | const F& cf = f; |
| 49 | assert(cf(ap, 2) == 'b'); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | template <class F> |
| 54 | void |
| 55 | test2(F f) |
| 56 | { |
| 57 | { |
| 58 | A a; |
| 59 | assert(f(a, 1, 2) == 'c'); |
| 60 | A* ap = &a; |
| 61 | assert(f(ap, 2, 3.5) == 'c'); |
Peter Collingbourne | a4c0d87 | 2014-01-22 22:56:52 +0000 | [diff] [blame] | 62 | const F& cf = f; |
| 63 | assert(cf(ap, 2, 3.5) == 'c'); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | int main() |
| 68 | { |
| 69 | test0(std::mem_fn(&A::test0)); |
| 70 | test1(std::mem_fn(&A::test1)); |
| 71 | test2(std::mem_fn(&A::test2)); |
| 72 | } |